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

Kyle Topasna
PLUS
Kyle Topasna
Courses Plus Student 2,468 Points

I'd like an admin to validate this block of exercises and challenge checks.

Exercise 2's instructions wanted to my set the default arguments to 5, but when I wrote my code that way, an error popped up saying to use 10.

Exercise 4 won't even let me advance. I used many different combinations of floats and integers in the exercise, but nothing worked.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you edit your question to show the code you used?

6 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I've just reverified the solutions below for Multiply View challanges 2 & 4

Challenge Task 2 of 5

Add a new route to multiply() that has two arguments. Add the same two arguments to the multiply() view. They should have defaults of 5.

from flask import Flask

app = Flask(__name__)

@app.route('/multiply')
@app.route('/multiply/<num1>/<num2>')
def multiply(num1=5, num2=5):
    # EDIT: changed to handle string values from URL
    result = int(num1) * int(num2)
    return str(result)

Challenge Task 4 of 5

Add routes to allow floats or a combination of floats and ints.

from flask import Flask

app = Flask(__name__)

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

Very confusing set of challenges. I used basically the same answers for all the challenges, because of the statements asked were not explicit about the type of data expected. This needs a whole redo, to minify the challenges and make it more clear what the defaults are and their type.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you say more about how you would like to see the challenge remade?

How did you come up with the single solution that solved all of the tasks without seeing all the tasks ahead of time?

While some solutions may work for all tasks of a challenge, it usually isn't until the solution for the last task is complete that the all-solving solution is available. The solution above for Task 4 of 5 (actually is the solution for Task 5 of 5) will work on all Tasks. However, one would not have come up with that solution given just the instructions for Task 1:

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.

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

Seeing only the final solution doesn't show the varied solution created through each step.

@app.route('/multiply')
@app.route('/multiply/<num1>/<num2>')
@app.route('/multiply/<int:num1>/<int:num2>')
@app.route('/multiply/<float:num1>/<float:num2>')
@app.route('/multiply/<int:num1>/<float:num2>')
@app.route('/multiply/<float:num1>/<int:num2>')

def multiply(num1=5, num2=5):
    #return '{}'.format(num1*num2)
    return str(num1*num2)

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.

After watching the video, I was in the mindset that there should be some type of input, not just a static product output of 25. From a continuity standpoint, it just more sense to process inputs, not a fixed product. I used num1 = 5, etc for the defaults. I would delete the first challenge and combine the middle the last 4 into 2 challenges.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Looking again at your code. The blank line between the app.route statements and the function def should be removed. Also, when the num1 and num2 values come from the URL they are strings and need to be converted to int in your return.

    return str(int(num1) * int(num2))
Alex Peck
Alex Peck
6,907 Points

I'm with Benjamin these challenges were confusingly validated.

Chris, In the solution you put forward for Challenge Task 2 of 5 the function returns the same result regardless of what variables are passed into it. It's just returning 5 times 5. If the function is being given values why would we hard code the result instead of multiplying the variables?

For example, this was my code and it doesn't validate:

from flask import Flask

app = Flask(__name__)

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

Screenshot of code above failing: https://www.evernote.com/l/AMdibLh8lnlL3ZrXVCg29HUdVWDj3OYCq6c

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Alex, the issue is when no arguments are provided, the parameters num1 and num2 get the default integer value 5. If they are assigned values from the URL, the values are strings, so you'll need to convert to int before the multiply.

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

Challenge 2 of 5's question is not clear and easily confused.

I have found that he meant you should add an extra route(in which case I did with floats instead of ints like the previous) and NOT change anything in the arguments of your function if it passed challenge 1. Here's my code that passed. This will also pass challenge3 without adjustments.

from flask import Flask

app = Flask(__name__)

@app.route('/multiply')
@app.route('/multiply/<int:num1>/<int:num2>')
@app.route('/multiply/<float:num1>/<float:num2>')
def multiply(num1 = 5, num2 = 5):
    return str(num1*num2)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Yes, the instructions could be clearer. As a general rule, the challenge code checkers reverify all tasks as you progress. So changing the routes, instead of adding routes, breaks the previous checks.