-1
import urllib2
import json

print "Hello and welcome to Currency Converter v0.1"

pounds = input("amount you wish to exchange: ")
url = "http://rate-exchange.appspot.com/currency?from=GBP&to=EUR"
if pounds.isdigit():
    print total
else:
    print "Please, use numeric input, we are not able to convert letters into"
    "money. (But that would be awesome!)"

response = urllib2.urlopen(url).read()
data = json.loads(response.decode('utf8'))
rates = data ['rate']
total = pounds * rates

This is my code. When I run this, I got this error:

   if pounds.isdigit():
AttributeError: 'int' object has no attribute 'isdigit' (when entering number into input)

or using different input:

   File "<string>", line 1, in <module>
NameError: name 'asedhfafsdg' is not defined (when entering letters in input)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Marks Gniteckis
  • 473
  • 1
  • 6
  • 18

2 Answers2

1

You are using input() where you should be using raw_input() instead.

In Python 2, input() is the same thing as eval(raw_input()); e.g. the input given by the user is executed as a Python expression. So if you enter text that can be executed as a valid Python integer literal, you'll get an int object, but if you enter asedhfafsdg then that's seen as a valid Python identifier and you get a NameError.

If you switch to using raw_input() instead you do have to convert the input to an integer explicitly:

pounds = input("amount you wish to exchange: ")
if pounds.isdigit():
    print total
    pounds = int(pounds)

You may also want to study this canonical Stack Overflow post about asking for user input: Asking the user for input until they give a valid response

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You can try this way also if you are expecting input as Integers only.

>>> try:
...     pounds = int(raw_input("amount you wish to exchange: "))
... except ValueError:
...     print "Please, use numeric input...."
... 
amount you wish to exchange: zero
Please, use numeric input....
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
  • Well i'm also using Python 2.7 here not 3. "int" type cast cast works on 2.7 also. – Tanveer Alam Oct 15 '14 at 12:22
  • Please see my answer, I had entered "zero" which is a string and it will show error for any string that's why I return a message in except clause. Please help me if its not good to handle it via exception handling? CTRL-C interrupts for input() also. – Tanveer Alam Oct 15 '14 at 12:34
  • Yes very true. Catching exception by just "except" also catches KeyboardInterrupt, SystemExit etc. If possible please edit my answer to be at least correct at some level or should I delete it? – Tanveer Alam Oct 15 '14 at 12:50
  • Well i tried "except ValueError:" but it is not handling for KeyboardInterrupt. I think it handles specifically for value error here. – Tanveer Alam Oct 15 '14 at 13:18
  • So is it okay if we don't handle KeyboardInterrupt here or should we ignore it and only handle ValueError. I am sorry if my reply were not that mature enough. – Tanveer Alam Oct 15 '14 at 13:24
  • @TanveerAlam: You seem to be missing the purpose of the `try..except` mechanism. We use `except` to catch errors that we know how to handle. All other errors *should* interrupt our code. – John Y Oct 15 '14 at 14:05
  • Yes John i'm focusing more on that now (digging more in exceptions :)) – Tanveer Alam Oct 16 '14 at 06:27
  • 1
    @TanveerAlam: much better. I cleaned up the comments now. I recommend you read through [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) as well; what you tried to cover here is handled in great detail there (plus much more). – Martijn Pieters Oct 16 '14 at 09:28