Reputation: 33
I have a simple try statement where i am assigning a variable to either x, o, or r (tic tac toe game i have made). The statements works if one of the three options is given, however, if it is something else given it says that it is returning a None object. I don't understand why it is not reassigning the variable to the new user input that is given on the second, third etc. run throughs
def chooseLetter(name):
try:
letter = input('Hello %s, would you like to be X or O or a random letter choice ("r") ' %name)
assert letter.lower() == 'x' or letter.lower()== 'o' or letter.lower() == 'r'
return letter
except AssertionError:
print('please choose either X or O ')
chooseLetter(name)
Upvotes: 0
Views: 87
Reputation: 77357
The problem is that you don't return the value in the error case
def chooseLetter(name):
try:
letter = input('Hello %s, would you like to be X or O or a random letter choice ("r") ' %name)
assert letter.lower() == 'x' or letter.lower()== 'o' or letter.lower() == 'r'
return letter
except AssertionError:
print('please choose either X or O ')
return chooseLetter(name)
Upvotes: 2