Avoiding accidental program exit - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Avoiding accidental program exit

yankorezzayankorezza Posts: 5Member
[b][red]This message was edited by yankorezza at 2004-9-30 6:47:43[/red][/b][hr]
I have a line in one of my programs that asks the user for a particular set of integars in order to create a desired range.
The line is simply this:-

num = input("Enter numbers for range (eg. 0,10): ")

To avoid more or less than 2 numbers being entered I have added the following code:-

j = 1
while j == 1:
num = input("Enter numbers for range (eg. 0,10): ")
numl = [num[0],num[1]]
if len(numl) < 2: j = 1
elif len(numl) > 2: j = 1
else: j = 0

The problem is that I want to guard against the program closing itself if a string is accidentally typed by the user at this point. Please bear with me as I am new to programming, but i have tried things like

elif num == str(); elif num != int(); elif str() in numl; etc.
Please can someone help me out on this because I can't figure it out?


Comments

  • infidelinfidel Posts: 2,900Member
    : [b][red]This message was edited by yankorezza at 2004-9-30 6:47:43[/red][/b][hr]
    : I have a line in one of my programs that asks the user for a particular set of integars in order to create a desired range.
    : The line is simply this:-
    :
    : num = input("Enter numbers for range (eg. 0,10): ")
    :
    : To avoid more or less than 2 numbers being entered I have added the following code:-
    :
    : j = 1
    : while j == 1:
    : num = input("Enter numbers for range (eg. 0,10): ")
    : numl = [num[0],num[1]]
    : if len(numl) < 2: j = 1
    : elif len(numl) > 2: j = 1
    : else: j = 0
    :
    : The problem is that I want to guard against the program closing itself if a string is accidentally typed by the user at this point. Please bear with me as I am new to programming, but i have tried things like
    :
    : elif num == str(); elif num != int(); elif str() in numl; etc.
    : Please can someone help me out on this because I can't figure it out?

    Ok, you've got a few problems here, I think. First, functions like str() and int() return new string and integer objects, respectively, so num == str() is basically asking if num is an empty string.

    numl = [num[0],num[1]] is kind of strange. If I type 1,10 then input() returns a tuple with two values like (1, 10). There's no need to make a list out of it like you have done. If you're worried about people entering too many values, you can use a slice like num[:2] which says give me the first two values of num.

    Also, there is a keyword, "break" which jumps out of a loop instead of using a sentinel value like you have. Although in this case I don't believe it's needed.

    You should also read up on exception handling. Exceptions can be trapped and dealt with using a "try" block.

    Putting all of these together, here is how I might code your loop:

    [code]
    def get_range():
    while True:
    try:
    nums = input("Enter numbers for range (eg. 0,10):")
    return nums[:2]
    except KeyboardInterrupt:
    raise
    except:
    print "Invalid entry, please try again.",

    try:
    x, y = get_range()
    print range(x, y)
    except KeyboardInterrupt:
    print "User cancelled operation."
    [/code]

    Let me know if any of this confuses you.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • yankorezzayankorezza Posts: 5Member
    Wow, that's really neat! Thanks again Infidel.
    Sorry about my terrible coding but I usually just experiment with stuff and see what happens!
    I don't understand the exception handling thing yet, but I will read up on it as you said.
    God bless the internet and yourself or I would be even worse than i am now!!


Sign In or Register to comment.