0

here is a beginner. I'm doing an exercise-code which tells you if you are poor, rich or neither rich nor poor depending on the value typed by the user. I would like to print an error message in case the user is typing a non int variable.... Of course it gives an error as I put int before the input function...here the code:

money=int(input())

if money <10000:
   print('you are poor '+name)
elif money >= 10000 and money<100000:
   print('you are neither poor nor rich '+ name)
elif money >=100000:
   print('you are rich ')

Could you show me how to do it?

1
  • 1
    Put money=int(input()) inside a try/catch and trap ValueError exceptions. Commented Jan 7, 2021 at 21:49

8 Answers 8

2

The best way would be to use what's know as a "try-except", which is where Python attempts to do what is inside the try block, but jumps out and runs what's in the except block instead if it hits an error.

while True: # Infinite loop
    try:
        money = int(input()) # Try to convert the input into a number
        break                # Break out of the infinite loop if the conversion is successful
    except ValueError:       # Do this instead if the try block causes a ValueError
        print("Sorry, that is not an integer. Please try again.")

if money < 10000:
    print('you are poor ' + name)
elif money >= 10000 and money < 100000:
    print('you are neither poor nor rich ' + name)
elif money >= 100000:
    print('you are rich ' + name)
Sign up to request clarification or add additional context in comments.

Comments

1

You can catch the exception which happens when you try to convert to integer:

try:
    money=int(input())
except ValueError:
    print("That is not an integer!")

Comments

1

Thanks a lot everybody for the answers! They were all good but the following code I guess was the best:

while True: # Infinite loop
    try:
        money = int(input()) # Try to convert the input into a number
        break                # Break out of the infinite loop if the conversion is successful
    except ValueError:       # Do this instead if the try block causes a ValueError
        print("Sorry, that is not an integer. Please try again.")

if money < 10000:
    print('you are poor ' + name)
elif money >= 10000 and money < 100000:
    print('you are neither poor nor rich ' + name)
elif money >= 100000:
    print('you are rich ' + name)

with the while loop the code stop to move on when the input is not a int. In the other cases, the try and except work but when the input is not an int , the code print the error message wanted but continue to run the if statement by generating a error message

Comments

1

One option is to use a try catch, that way you can handle the error.

Something like this:

try:
    money=int(input())

    if money <10000:
        print('you are poor '+name)
    elif money >= 10000 and money<100000:
        print('you are neither poor nor rich '+ name)
    elif money >=100000:
        print('you are rich ')

except:
    print("Input is not a valid number")

4 Comments

I tried but didn't work. I copied and pasted the code from thatOtherAndrew and it was working
This does not prevent the code to try to use the variable even though there was an exception. There should be some code preventing the rest of the execution after the exception is caught - for example an else:, or a return or a break, depending on where this whole thing is.
oh yes, thank you. fixed. Although by now I see this is pretty much what 5 other answers already say :)
And your while loop solution works great too
0

Use exceptions to handle the input of such values

try:
    money=int(input())

    if money <10000:
       print('you are poor '+ name)
    elif money >= 10000 and money<100000:
       print('you are neither poor nor rich '+ name)
    elif money >=100000:
       print('you are rich ')
except:
    print("error")

Comments

0

try to get an integer for input() and send to money;

try:
money=int(input())

then, you will need an except, in order to handle error;

except:
print('You should enter an integer!')

Comments

0

you need to you the else condition and for

ValueError

you need a try,except block

try:
    money=int(input())
    
    if money <10000:
       print('you are poor '+name)
    elif money >= 10000 and money<100000:
       print('you are neither poor nor rich '+ name)
    elif money >=100000:
       print('you are rich ')
    else:
       print('wrong value')
except ValueError:
    print("not an integer")

2 Comments

didn't work because even if I put a non int value , money=int(input()) convert it in a int....right?
encapsulate the whole with the try,except block
0

As you have already use int function before input it will only accept integer value.

In case user input a string value, use the try except to print the error messege. This will prevent your program from getting crashed.

try:
   money=int(input())

   if money <10000:
       print('you are poor '+name)
   elif money >= 10000 and money<100000:
       print('you are neither poor nor rich '+ name)
   elif money >=100000:
       print('you are rich ')

Except:
   print("Please enter valid data")

Please take care of indentation.

Golden Tip-- Instead of int use float as it will accept the decimal value too

Comments

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.