Games of chance, mixing up int and str

I’m currently working on the roulette section of Games of Chance, found here: https://www.codecademy.com/practice/projects/games-of-chance

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?

You could provide an error in your code if the user provides thing other than ‘odd’ and it’s variations or ‘even’ and its variations. For example:

if guess != 'odd' or 'even':
return "Incorrect guess. Try again."

I hope this helps!
Happy coding!

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

you would need to apply != to 'even' as well (but then you get multiple NOT in your expression so you may want to change how you express it as well)

Oops, yes, sorry, bad syntax on my behalf!

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."

Sorry for the miscommunication!
Happy coding!

Oooh I see, that makes sense. That’s a good way to go about it, thanks!

It 100% fixed it lol

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.

I changed it for even as well, you’re right

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.