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

Bummer! Didn't get a 200 at /multiply

What is wrong with my code?

from flask import Flask

app = Flask(name)

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

flask_app.py
from flask import Flask

app = Flask(__name__)

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

7 Answers

Dan Johnson
Dan Johnson
40,533 Points

You'll want to have a new route expecting two variables rather than them being hard coded in.

You can do that like this:

@app.route("/multiply")
@app.route("/multiply/<num1>/<num2>")
def multiply(num1=5, num2=5):
  #Not updated until later in the challenge.
  return str(5 * 5)

I believe what Kenneth is saying is that this code will get you through up to Challenge 4 of 5:

from flask import Flask

app = Flask(__name__)

@app.route("/multiply")
@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):
  #Not updated until later in the challenge.
  return str(5 * 5)

However, then for Challenge 5 of 5 the return has to updated:

from flask import Flask

app = Flask(__name__)

@app.route("/multiply")
@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 str(num1 * num2)
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Also, the view just wants back the result of 5 * 5, not the mathematical sentence.

Avik Chakraborty
Avik Chakraborty
6,621 Points

Step1 from flask import Flask

app = Flask(name)

@app.route('/') @app.route('/multiply')

def multiply(): return str(5*5)

Still having problems, keep getting my code takes to long or Didn't get a 200 at multiply

from flask import Flask
from flask import request

app = Flask (__name__)


@app.route('/')
def index (name="Treehouse"):
    name = request.args.get('name',name)
    return "Hello from {}".format (name)

app.run(debug=True, port=8000,host='0.0.0.0')
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Take out the app.run(). Someone else reported to me that that caused challenges to time out.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, you also didn't create a route at /multiply like the instructions tell you to:

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.

Having problems with this problem

from flask import Flask
from flask import request

app=flask(__name__)

@app.route('1')
def index (name="Treehouse"):
    return"Hello from {}". format(name)
name=request,args.get(name,name)

app,run(debug=True,port=8,000,host='0.0.0.0'
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

So, a few problems.

  1. You want to use the Flask class that you imported for your app variable. app = Flask(__name__)
  2. Your route should start with a /
  3. You have a lot of spacing issues. Don't put spaces before or after dots, before or after parentheses, and don't leave spaces out after keywords like return.
  4. You won't want to have your return before you do variable assignment and you'll want the variable assignment (the line starting with name=) to be indented inside your function.
  5. You want app.run not app,run.
  6. You don't need a comma inside 8000 when it's the number.

thanks Kenneth completed that challenge