-3

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Kirtan
  • 1
  • 1
  • 1
    The dot in `2.5` is not a digit, hence `user_input.isdigit()` returns `False` - obviously this check is wrong for what you are trying to do – UnholySheep Aug 21 '23 at 07:47
  • 1
    `"2.5".isdigit()` is `False`. You need different validation if you want to support floats. – Gino Mempin Aug 21 '23 at 07:47
  • 1
    "but I’m just prompted with the « isfloat and isnumeric don’t work for strings » error" no you're not, I just did `print("3.14159".isnumeric())` – roganjosh Aug 21 '23 at 07:47
  • What is wrong with your code? I can see it's working fine with integers, floats and strings. If you want to find float, check this [SO answer](https://stackoverflow.com/a/20929881/8353711) – shaik moeed Aug 21 '23 at 07:47
  • *"I’m just prompted with the « isfloat and isnumeric don’t work for strings »*" What's the `isfloat` you are referring to here? Python strings have an `isnumeric` method, but not `isfloat`. – slothrop Aug 21 '23 at 08:16

1 Answers1

-1

The issue you're facing is due to the fact that isnumeric() and isdigit() methods only work for checking if a string represents a positive integer. They don't handle decimal numbers or negative numbers. To handle float values as well, you need to use a different approach.

Here's how you can modify your code to handle float inputs:

# Checks if the user's answer can be used by the determine_reply() 
# function, and if it cannot provides an error.
def get_reply(user_input):
    try:
        user_input_float = float(user_input)
        return determine_reply(user_input_float)
    except ValueError:
        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_float):
    if user_input_float > 2:
        return 'Wow, that\'s a lot of coffee!'
    elif user_input_float == 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)

In this updated code, the try block attempts to convert the user input to a float using float(user_input). If the conversion is successful, it then passes the float value to the determine_reply() function. If the conversion raises a ValueError (meaning the input is not a valid number), the code catches the error and returns the appropriate error message.

This way, your program will be able to handle both integer and float inputs, providing the appropriate response based on the user's input.