This program I made asks for the number of cups of coffee the user drank, and based on if the number is greater than 2, it gives a response. My problem is, if I input something like 2.5, it doesn’t detect that as a valid answer and it goes to the else statement, even though float values are perfectly okay for the program. How do I fix this?
# Checks if the user's answer can be used by the determineReply()
# function, and if it cannot provides an error.
def get_reply(user_input):
if user_input.isdigit():
user_input_int = int(user_input)
return determine_reply(user_input_int)
else:
return 'Sorry, I don\'t understand your answer. I was looking for a number, not a string.'
# Determines the correct reply
def determine_reply(user_input_int):
if user_input_int > 2:
return 'Wow, that\'s a lot of coffee!'
elif user_input_int == 0:
return 'Should we go grab a coffee? I could use one too.'
else:
return 'Sounds like the right amount of coffee to start the day.'
# Ask for user input
user_coffee_input = input('How many cups of coffee have you had today? ')
# Process the answer to get the right reply, and print that reply
reply = get_reply(user_coffee_input)
print(reply)
I tried isfloat()
and isnumeric()
, but I’m just prompted with the « isfloat and isnumeric don’t work for strings » error. What’s the right way to approach this?