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
behar
10,800 PointsQuestion about function variables
So im trying to create a Tic Tac Toe game.. In the following code im trying to ask the user if he wants to play first or second. I want to ensure that he only input integers, and that the integers are between 1 and 2. However when i run this code, it says that first_or_second is not defined? Why is this, the user has just inputted what first_or_second is??
def Input():
try:
first_or_second = int(input("Would you like to go first or second? 1/2"))
except ValueError:
print("You can only input numbers!")
print("Welcome to a Tic Tac Toe game! You will play as X!")
Input()
if first_or_second not == 1 or 2:
print("You can only input 1 or 2!")
Input()
else:
print("Ok youre playing as {}".format(first_or_second))
2 Answers
Ben Reynolds
35,170 PointsIt's probably a scope issue, first_or_second is declared inside the Input() function, so its value is only accessible to other code within that function. Try declaring it before the Input function.
Emerson Rubio
18,024 Pointsfirst_or_second is declared inside a function meaning the rest of your code doesn't have access to it. As Ben said, it's a scope issue.