473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Try: Except: not catching ValueError (Help)

Eclipse
G'day all

In the code below I am trying to catch an error thrown if i enter any other value than an integer. (I used the string 'six' and the float '1.5')

The error thrown in the command window by the code is:

Traceback (most recent call last):
File "C:\Documents and Settings\Smythville\My Documents\Python\Stuff\Ch4\ex1-6.py", line 59, in <module>
a = int(raw_input("\nTry a number!"))
ValueError: invalid literal for int() with base 10: 'six'

Expand|Select|Wrap|Line Numbers
  1.  
  2. a = int(raw_input("\nTry a number!"))
  3.  
  4. try:
  5.   if a >= 0 and a <= 9:
  6.     print "\nYour number is between or equal to 0 and 9."
  7.   else:
  8.     print "\nYour number is not between or equal to 0 and 9."
  9. except ValueError: 
  10.   print "Could not convert data to an integer."
  11.  
Even though I have specified the ValueError is doesn't catch it.

Any Ideas?

Thanks

Eclipse
Jan 2 '08 #1
1 3150
bvdet
2,851 Expert Mod 2GB
G'day all

In the code below I am trying to catch an error thrown if i enter any other value than an integer. (I used the string 'six' and the float '1.5')

The error thrown in the command window by the code is:

Traceback (most recent call last):
File "C:\Documents and Settings\Smythville\My Documents\Python\Stuff\Ch4\ex1-6.py", line 59, in <module>
a = int(raw_input("\nTry a number!"))
ValueError: invalid literal for int() with base 10: 'six'

Expand|Select|Wrap|Line Numbers
  1.  
  2. a = int(raw_input("\nTry a number!"))
  3.  
  4. try:
  5.   if a >= 0 and a <= 9:
  6.     print "\nYour number is between or equal to 0 and 9."
  7.   else:
  8.     print "\nYour number is not between or equal to 0 and 9."
  9. except ValueError: 
  10.   print "Could not convert data to an integer."
  11.  
Even though I have specified the ValueError is doesn't catch it.

Any Ideas?

Thanks

Eclipse
Move the first line of code inside the try block.
Jan 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
2
by: Mike Hoy | last post by:
hi my goal is to handle the problem of a user typing a letter or symbol rather than a number I found something called try: and ValueError: Anyway this is what i attempted: #menuloop...
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
12
by: Carl J. Van Arsdall | last post by:
Hey python people, I'm interested in using the try/finally clause to ensure graceful cleanup regardless of how a block of code exits. However, I still am interested in capturing the exception....
13
by: John Salerno | last post by:
The code to look at is the try statement in the NumbersValidator class, just a few lines down. Is this a clean way to write it? i.e. is it okay to have all those return statements? Is this a good...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
35
by: Arnaud Delobelle | last post by:
Hi all, Imagine I have three functions a(x), b(x), c(x) that each return something or raise an exception. Imagine I want to define a function that returns a(x) if possible, otherwise b(x),...
7
by: oscartheduck | last post by:
I have a small script for doing some ssh stuff for me. I could have written it as shell script, but wanted to improve my python skills some. RIght now, I'm not catching a syntax error as I'd...
3
by: Patrick C | last post by:
Hey, If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop? If so, is there any way i can tell it to stay in the while loop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.