I want the program to interpret “odd” and “Odd” and “ODD” all as the same answer, so whenever I compare the user’s guess to the number that came up, I set it as guess.lower(). Ok.
But when the user guesses an INT instead of a STRING, the program is giving me this error:
File “script.py”, line 115, in roulette
elif(guess.lower()==“high” and ball >=19 and ball != 37):
AttributeError: ‘int’ object has no attribute ‘lower’
I can fix it by adding in “type(guess) == str and guess.lower() == etc” to the elif statement but that seems kind of excessive? Is there a more elegant way to do it?
But I want the user to be able to go with “ODD” and it still work. Maybe I’m misunderstanding your suggestion, but in your case “ODD” is not equal to “odd,” so even if the number was 3 it would say it was incorrect.
if it’s supposed to be string then testing whether it’s string doesn’t make it a string, that doesn’t fix it, does it? what you would fix is how you obtained it
I meant you could change the ‘ODD’ to lowercase .lower() before applying the if...else statement. This would mean that even if the user put ‘ODD’ in the if...else, it would still be ‘odd’.
E.g.
user_guess.lower()
if user_guess != 'odd' or user_guess != 'even':
return "Incorrect guess. Try again."
I think because it was attempting to .lower() on an int and throwing an error. But if it never looked at ints, only strings (because sometimes I put in strings, sometimes I put in ints), then it never threw the error.
You have some source of a value, and from that source you’re getting either an int or string
If you read input, you get a string. How would that sometimes be an int?
Very few things are meaningfully represented as either string or int. If you’re reading user input (text) and comparing it to specific text, then int has nothing to do with that, it would be wrong, that’s why it wouldn’t be fixed, because having an int would be wrong. Even if the user types in a number, that is still text.
You’d be able to test whether you have the thing you expected (a string) but if it’s not a string, then the problem remains, that there was no string.