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

Task 5 of 5: Multiply 5*5. I have clearly defined num1 but continue to get an error...

"Bummer: NameError: name 'num1' is not defined"

flask_app.py
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,product=num1*num2):
    return '{} * {} = {}'.format(num1, num2, product)

3 Answers

Got the correct answer:

def multiply(num1 = 5, num2 = 5):
    product = num1 * num2
    return '{}'.format(product)
Anthony Crespo
seal-mask
.a{fill-rule:evenodd;}techdegree
Anthony Crespo
Python Web Development Techdegree Student 12,973 Points

Oh yeah you right! It has to return only the product and not the whole mathematical formula. Sorry lol I corrected your code but didn't read the challenge.

You can even drop the product variable and make it shorter.

def multiply(num1 = 5, num2 = 5):
    return f'{num1 * num2}'

Anthony, thats pretty cool.

The lone 'f' allows you to perform a mathematical formula within the curly brackets I presume?

return f'{num1 * num2}'
Anthony Crespo
seal-mask
.a{fill-rule:evenodd;}techdegree
Anthony Crespo
Python Web Development Techdegree Student 12,973 Points

It's the new fstring (Python 3.6). It work the same as .format() but it's way shorter.

num1 = 5
num2 = 5
string = f'{num1} + {num2} = {num1 * num2}'
Anthony Crespo
seal-mask
.a{fill-rule:evenodd;}techdegree
Anthony Crespo
Python Web Development Techdegree Student 12,973 Points

I don't think you can multiply local variable in there. (Maybe, I just don't know how?) If it's inside the function it works:

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

The way I understand it's that the arguments don't exist outside the scope of the body of the function cause if you use global variables it works:

a = 5
b = 5
def multiply(num1=5,num2=5, product = a * a):
    return '{} * {} = {}'.format(num1, num2, product)

Hey Anthony C., I think the question may be looking for something else because I'm still getting error messages...

'Bummer: Didn't get the expected response. Got 5 * 5 = 25.'

The quiz is telling us here that it doesn't want '5 * 5 = 25' as an answer. What solution could it be looking for?

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