Avoiding accidental program exit

[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:-

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

: [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.

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!!