Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Welcome to Flask Multiply View

Ping Li
Ping Li
2,689 Points

What did I do wrong

Add a view named multiply. Give multiply a route named /multiply. Make multiply() return the product of 5 * 5. Remember, views have to return strings.

flask_app.py
from flask import Flask

app = Flask(__name__)

@app.route('/multiply/<int:num1>/<int:num2>')
def multiply(num1, num2):
    num1 = 5
    num2 = 5
    return '{} * {} = {}'.format(num1, num2, num1*num2)

2 Answers

Hey Ping!

Let's take a look at what the task is asking you to do. It wants you to add a view called multiply, give it a route '/multiply' and make it return the product of 5 * 5. So, first, let's create the correct route:

@app.route('/multiply')

Since it's asking you to assign values (5 * 5) to the variables, there is not need to set <num1>/<num2> at the route. Then, we can create the view:

@app.route('/multiply')
def multiply(num1=5, num2=5):
    return str(num1 * num2)

The thing to pay attention here is that you can only return strings in flask. So you have to parse the result for a string. And also, remember to assign the values 5 to both nums, since that's what the task is asking.

Cheers!

Marta P.
Marta P.
2,849 Points

Hi! I tried your solution but it doesn't seem to pass! Ideas?:)

Hey Marta, this code is passing:

from flask import Flask

app = Flask(__name__)

@app.route('/multiply')
@app.route('/multiply/<int:num1>/<int:num2>')
def multiply(num1=5, num2=5):
    result = num1 * num2
    return str(result)