0

I am new to programming and trying to make the user input accept both integer and strings. I have tried the following coding.. but not sure where I am going wrong. Any suggestions?

    noAdults = input("Please enter the number of adults:")
    while noAdults.isalpha():
        print("Number must be valid and >= 0")
    if noAdults.isdigit() and noAdults < 0:
        print("Error")
        noAdults = input("Please enter the number of adults:")

ValueError: invalid literal for int() with base 10:

I am guessing there is a ValueError because I have used the variable noAdults with isalpha and is making an error because it is in an int?

1
  • That current code isn't going to check again for the new noAdults if it's got a letter in it. Commented May 6, 2014 at 4:52

2 Answers 2

2

You need to verify that the input string is valid integer before you can check if it is non-negative

while True:        
    noAdults = input("Please enter the number of adults:")
    try:
        noAdults = int(noAdults)
    except ValueError:
        print("Not a valid number")
        continue
    if noAdults >= 0:
        break
    else:
        print("Number must be >= 0")
Sign up to request clarification or add additional context in comments.

2 Comments

This did work.. However it did not give the prompt message of "number must be valid.." when entering in a new number. I tried to add it in, but I'm so confused.
I haven't moved to Python 3 yet, so I'm still getting in the habit of using the print function instead of the print statement.
0

It's throwing the exception before you handle the bad case of string inputs, it fails on the first line. You can check isalpha() before attempting the int(), or you can catch the ValueError exception.

To expand on chepner's solution, with a few extra print lines to show you what is going on:

while True:
    noAdults = input("Please enter the number of adults:")
    try:
        noAdults = int(noAdults)
    except ValueError:
        print("Not a valid number")
        continue
    if noAdults >= 0:
        break
    else:
        print("Not greater than 0")

print(noAdults)

This is a fully working version in Python 3.

5 Comments

I tried to check isalpha() first before doing the int.. I got the is alpha to return the error message but I was unable to get the int to return.. still returned an error. I have updated my code.
You need to convert it to an int like in chepner's code.
I don't understand what you mean. Are you talking about the try method? noAdults= int(noAdults)
Yes, you're not converting it to a number.
Thank you. This works and does everything I want it to do. However, not sure I am allowed to use break. I will work on it. Thank you again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.