2

I need to add an error message if the user enters a string instead of an integer in my menu selection, and also amounts that the user input as data. I tried this code but it doesn't work.

import sys
try:
    newamount=int(input('Enter the new amount:'))
except ValueError:
    print ("error")

What am I doing wrong?

1
  • Two things: you don't need to import sys to use input() or raw_input(), and if you are using python 2.7.x, please do not use input() for string input from users, use raw_input(). input() evaluates the code passed to it, so if you do this a malicious user could do something like "import os; os.system("rm -rf /")" or something equally dangerous and blow up your system. input() is ONLY safe in python 3.x, NEVER use it in python 2.7.x. Commented Oct 24, 2013 at 3:34

3 Answers 3

2

That's because passing an invalid string (not a number) to int() will raise a ValueError, and not TypeError. You're close though.

Just change it and it should work great.

except ValueError:
    print('Error!')

If you want to do something with the newamount variable, i suggest you to do it in the try block:

try:
    newamount=int(input('Enter the new amount:'))
    tempfunction = amount + newamount

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

tempfunction = amount + newamount # adding new amount to the previous amount UnboundLocalError: local variable 'newamount' referenced before assignment >>>
In your try statement?
i'm trying to ask user to input amounts and if user enter any strings i need to print error and re appear the newamount=int(input('Enter the new amount:'))
1

TypeError would be raised if the parameter to int() was of the wrong type.

Assuming you are using Python3, the return value of input() will always be type str

ValueError is raised if the type is ok, but the content isn't able to be converted to an int.

To ask over and over, you should use a while loop

while True:
    try:
        newamount=int(input('Enter the new amount:'))
        break
    except ValueError:
        print ("error")

If you want to keep count of the errors, use itertools.count and a for loop

from itertools import count
for c in count():
    try:
        newamount=int(input('Enter the new amount:'))
        break
    except ValueError:
        print ("error", c)

6 Comments

ya its python 3 and hw do i fix this?
@user2912389, the error you mention in the comments of alKid's answer it not from the code you have here. It's because of the way you are using the variable in a function. You should ask a new question and post the full code there.
@user2912389, do you still have a problem after replacing TypeError with ValueError?
How does it crash? Do you mean you want to ask over and over until they enter an integer?
@user2912389, ok You need a while loop. I added it to my answer
|
1

I think its better to use raw_input in these cases where the input is to be eval manually. It goes like this...

s = raw_input()
try:
    choice = int(s)
except ValueError:
print ('Wrong Input!')  

5 Comments

Given the parens on print, he's likely using Python3 which doesn't have raw_input (input has the old raw_input behavior)
ya python 3 .what are differentes should i do?
But thats not the case we can use parenthesis even in earlier version of python. here is the proof.. Python 2.4.3 (#1, Oct 23 2012, 22:02:41) [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print('hi') hi
Yep. But it isn't needed, and it's unlikely anyone would do that for printing string.
I am not a mind reader to alKid to know what version he is using unless he specifies it :)

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.