'How to display a message when a string is input instead of an int?
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?
Solution 1:[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!")
Solution 2:[2]
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
Solution 3:[3]
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")
Solution 4:[4]
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")
Solution 5:[5]
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!')
Solution 6:[6]
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")
Solution 7:[7]
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
Solution 8:[8]
It may be a bit risky but it has worked for me in the past. I suggest creating a copy of your project in another folder and trying this safely away from the original project. Also if you are using a postgresql database then just switch to a dummy database although it shouldn't make a difference but just to be on the safer side.
Inside of your migrations folder, inside the app folder, try deleting the all the files inside of pycache EXCEPT init.cpython-39.pyc and 0001_initial.cpython-39.pyc and inside of the migrations folder delete all files EXCEPT init.py and 0001_initial.py.
IMPORTANT: Delete from the pycache INSIDE of migrations NOT the one outside of it.
Solution 9:[9]
The approach below has often helped me out, if its a production database its good to backup and also your migrations folder before getting started.
- Start by deleting your current applications migrations folder
- temporarily add a field to your models.
- Makemigrations then migrate
- Delete the temporarily added field from your model
- Makemigrations and migrate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zvone |
| Solution 2 | Giacomo Bonvini |
| Solution 3 | |
| Solution 4 | NewGCorp |
| Solution 5 | Melisa Özen |
| Solution 6 | |
| Solution 7 | Nimantha |
| Solution 8 | lakimapturn |
| Solution 9 |
