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?

HansUp
  • 95,961
  • 11
  • 77
  • 135
user2912389
  • 357
  • 3
  • 10
  • 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. – reem Oct 24 '13 at 03:34

3 Answers3

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!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • tempfunction = amount + newamount # adding new amount to the previous amount UnboundLocalError: local variable 'newamount' referenced before assignment >>> – user2912389 Oct 24 '13 at 02:41
  • In your try statement? – aIKid Oct 24 '13 at 02:42
  • 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:')) – user2912389 Oct 24 '13 at 02:52
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)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
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!')  
Sravan K Ghantasala
  • 1,058
  • 8
  • 14
  • 1
    Given the parens on `print`, he's likely using Python3 which doesn't have `raw_input` (`input` has the old `raw_input` behavior) – Free Monica Cellio Oct 24 '13 at 03:05
  • ya python 3 .what are differentes should i do? – user2912389 Oct 24 '13 at 03:09
  • 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 – Sravan K Ghantasala Oct 25 '13 at 02:40
  • Yep. But it isn't needed, and it's unlikely anyone would do that for printing *string*. – aIKid Oct 27 '13 at 09:01
  • I am not a mind reader to alKid to know what version he is using unless he specifies it :) – Sravan K Ghantasala Oct 27 '13 at 09:19