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
zachary hatsis
1,072 PointsFunctions: Dumb Question
Why is it that when i execute this code on this site (http://www.pythontutor.com/visualize.html), it says global name 'input' is not defined ?
import random
def main():
guess = int(input("Enter a number from 1 - 10 ? "))
mynum = random.randint(1, 10)
def dothemath():
if guess > mynum:
print ("guess again")
main()
elif guess < mynum:
print ("guess again")
main()
elif guess == mynum:
print ("Congratulations! we both guessed {}".format(mynum))
else:
print ("Please pick a number from 1 to 10.")
main()
dothemath()
main()
2 Answers
William Li
Courses Plus Student 26,868 PointsRun your code in Python 3.3, it'll work fine.
If you're going to run the code in python 2.7. Change that line into
guess = int(raw_input("Enter a number from 1 - 10 ? "))
Hanley Chan
27,771 PointsIt works fine for me when I changed the language from Python 2.7 to 3.3