LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2008, 10:47 AM   #1
raskol
Member
 
Registered: Mar 2008
Posts: 51

Rep: Reputation: 15
Input in integers, users writes a char, how to catch exception?


edit: sorry i didnt mention this is PYTHON. and it is intended but the forum formats it to being unintended.

i have (nonsense)program like this:

anbr=input("input another number: ")
if anbr < 10:
print "your input + 5 is"
print anbr+5
elif anbr == 22:
print "wow 22 u say"
else:
print "hahaha"
raw_input()

if i input "a" instead of an integer it crashes.

how do i catch an exception like this? while anbr!= (int) or something?



or in this case how to do it?

ca = input("guess wrong or live now: ")
tc = {1: "u diieee",
2: "lol feel the pain",
3: "muppet"}
print tc.get(ca, "congrats u live fish")

Last edited by raskol; 03-30-2008 at 11:53 AM.
 
Old 03-30-2008, 11:38 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Use one of the scanf()/fscanf()/sscanf() family of functions, with a format string to scan an integer. It returns the number of successfully scanned arguments. If that number is less than 1, it was not able to convert the input stream as specified.
--- rod.
 
Old 03-30-2008, 11:41 AM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
For Python, you need to put your code in CODE blocks to preserve indentation. I'm assuming you meant:

Code:
anbr = input("input another number: ")
if anbr < 10:
    print "your input + 5 is"
    print anbr + 5
elif anbr == 22:
    print "wow 22 u say"
else:
    print "hahaha"
raw_input()
You can deal with invalid input like this:

Code:
def input_int(prompt=""):
    """
    Ask the user to enter an integer and return the value entered.
    If the input is invalid, ask again.
    """
    while True:
        try:
            return int(raw_input(prompt))
        except ValueError:
            print "You must enter an integer."

anbr = input_int("input another number: ")
Note that I used int(raw_input(prompt)) instead of input(prompt). This is because the input function is unsafe: What if the user types __import__('os').remove('some_important_file')?
 
Old 03-30-2008, 11:43 AM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by theNbomr View Post
Use one of the scanf()/fscanf()/sscanf() family of functions, with a format string to scan an integer. It returns the number of successfully scanned arguments. If that number is less than 1, it was not able to convert the input stream as specified.
--- rod.
That would work, except for the fact that the code isn't written in C
 
Old 03-30-2008, 12:28 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
read it using raw_input() and use .isdigit() to check if it's an int before converting it.
example:
Code:
anbr = raw_input("input another number: ")
while not anbr.isdigit():
    anbr = raw_input('"%s" is not a number, input a NUMBER: ' % anbr)
anbr = int(anbr)
if anbr < 10:
    print "your input + 5 is"
    print anbr + 5
elif anbr == 22:
    print "wow 22 u say"
else:
    print "hahaha"
raw_input()
EDIT: Be careful when using eval it can cause problems. input() uses eval.

Last edited by angrybanana; 03-30-2008 at 12:31 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C how to check if the input of scanf is an integer as opposed to a char? sycamorex Programming 1 02-07-2008 04:00 PM
keytool error: java.lang.Exception: Input not an X.509 certificate ChrisScott Linux - Server 1 09-13-2007 02:48 AM
I Need a program do try-catch for Exception In Linux? saeedeh1363 Programming 2 09-04-2007 05:49 AM
Java: try won't catch exception nro Programming 3 09-15-2004 11:28 PM
count char on input centr0 Programming 7 07-06-2003 12:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration