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
Mia Allen
2,929 PointsWhy isn't this python flask code working?
Whenever I go to port 8000, it just says "hello from treehouse!". I tried deleting the code that said that, but then it gave me an error. What is wrong with my code?
from flask import Flask from flask import render_template
app = Flask(name)
@app.route('/') @app.route('/<name>') def index(name="Treehouse"): return "Hello from {}".format(name)
@app.route('/add/<int:num1>/<int:num2>') @app.route('/add/<float:num1>/<float:num2>') @app.route('/add/<int:num1>/<float:num2>') @app.route('/add/<float:num1>/<int:num2>') def add(num1, num2): return render_template("add.html", num1=num1, num2=num2)
app.run(debug=True, port= 8000, host='0.0.0.0')
and this is add.html:
<!doctype html> <html> <head><title>Adding!</title></head> <body> <h1>{{num1}} + {{Num2}} = {{num1 + num2}}</h1> </body> </html>
2 Answers
ursaminor
11,271 PointsYou're getting the expected output. The index route ('/') returns "Hello from {}".format(name). When you don't pass in a name you get "Hello from Treehouse" because The default is "Treehouse". def index(name="Treehouse") Add "/add/2/3" to the url to see the output for the add view.
Please put code in a code block (surrounded by 3 back tics ```) so we can read it. The way you have it, it's hard to read and we can't tell if you have any indentation errors. Click on the Markdown Cheatsheet below the comment input field to if you need help. Take a look at some other questions to see examples of code blocks.
Mia Allen
2,929 PointsThank you so much!