473,326 Members | 2,061 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,326 software developers and data experts.

try: except: ValueError

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
menuChoice = 7
printMenu()
while menuChoice != 0:
try:
menuChoice = int("1 = write | 2 = view | 0 = quit"))
except ValueError:
print "enter a number"

if menuChoice == 1:
writeEntry()
elif menuChoice == 2:
viewEntry()
elife menuChoice == 0:
pass
else:
printMenu()

print "bye"

this works as though I never added try: and except ValueError: at all.
the program just doesn't use them at all.

if a user types a&s it still errors 'a&s' is not defined

what am i doing wrong here?

Jul 19 '05 #1
2 4069
Mike Hoy wrote:
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
menuChoice = 7
printMenu()
while menuChoice != 0:
try:
menuChoice = int("1 = write | 2 = view | 0 = quit"))
except ValueError:
print "enter a number"


presumably you meant menuChoice = input("1 = write | 2 = view | 0 = quit").
input() evaluates the user's input similarly to what the interactive interpreter does, so if you type something like a&s you get a NameError - input() is trying to evaluate the expression 'a&s', it looks for a variable named 'a'.

The solution is to use raw_input() which returns the user input as a string. Then you can do whatever validation you wish, e.g.
menuChoice = int(raw_input("1 = write | 2 = view | 0 = quit"))
which will raise an exception if the input is not a valid integer.

Kent
Jul 19 '05 #2
The solution is to use raw_input() which returns the user input as a string. Then you can do whatever validation you wish, e.g.
menuChoice = int(raw_input("1 = write | 2 = view | 0 = quit"))
which will raise an exception if the input is not a valid integer.

yes that did it, thanks

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
5
by: ' | last post by:
the code below will not execute the except section when i enter a number. what am i missing ? ######################################### ..while 1: .. print 'Pump Protection ? ' .. #line...
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()
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...
1
Eclipse
by: Eclipse | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.