Try Except If

This is the place for queries that don't fit in any of the other categories.

Try Except If

Postby baum » Sun Feb 09, 2014 6:14 pm

hello everybody

i m new to python and have a background in c and a little java.

i got stuck at handling userinputs. my goal is to have the user input either 0 or 1 and if the input is 2 or more or -1 or less the user gets directed back to the input request. otherwise the value gets processed further.

i got it already working:
Code: Select all
val = 0

while True:
    while True:
        try:
            val = int(input("Enter number: "))
            break

        except ValueError:
            print("no numba")
            continue

    if val < 0 or val > 1:
        print("no Option")

    else:
        print("ok")
        break




My question is:
Is it possible to leave out the first while loop and add a second except with an if statement like:

Code: Select all
val = 0
while True:
    try:
         val = int(input("Rein damit: "))
         break

     except ValueError:
         print("kai Zahl")
         continue
     
     except if val < 0 or val > 1:
         print("no Option")
         continue
 
     else:
        print("ok")
        break


i found some things on the internet but i think i didnt formulate the queston the right way.

best regards

baum
Last edited by Yoriz on Sun Feb 09, 2014 6:19 pm, edited 1 time in total.
Reason: First post lock
baum
 
Posts: 3
Joined: Sun Feb 09, 2014 5:09 pm

Re: Try Except If

Postby Yoriz » Sun Feb 09, 2014 6:42 pm

It is possible but you cant have a condition after an except, it has to be an exception.
In the follwing code i created a exception that can be raised if the value is not 0 or 1.
Code: Select all
class EntryNotZeroOrOne(Exception):
    pass

while True:
    try:
        val = int(input("Enter number: "))
        if not 0 <= val <= 1:
            raise EntryNotZeroOrOne()
       
        print("ok")
        break

    except ValueError:
        print("Please enter an int")

    except EntryNotZeroOrOne:
        print("Please enter an int of 0 or 1")
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Try Except If

Postby baum » Sun Feb 09, 2014 6:56 pm

hey thanks now that works. do you think this is the most simple and elegant way to do handle that?
baum
 
Posts: 3
Joined: Sun Feb 09, 2014 5:09 pm

Re: Try Except If

Postby Yoriz » Sun Feb 09, 2014 7:00 pm

This would be simpler
Code: Select all
while True:
    try:
        val = int(input("Please enter 0 or 1: "))
        if val in (0, 1):
            print("ok")
            break

    except ValueError:
        pass
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Try Except If

Postby baum » Sun Feb 09, 2014 7:04 pm

perfect! thank you
baum
 
Posts: 3
Joined: Sun Feb 09, 2014 5:09 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests