1

I was just trying to understand Try and except statements better.I am stuck. So I was hoping if you guys could clarify. This program purely for learning.

Appreciate your input.

while True:
    x=int(input('enter no.->'))

    try:
        x/2
    except ValueError:
        print('try again')
    else:
        if (x/2)==1:
            break
print('program end')

So i wrote this program with the intention-

  1. loop if x is a number
  2. if it is not. then 'except' comes into play and starts again
  3. if quotient is 1. STOP.

Even if I change it to

    x=input('enter no.->')

    try:
        int(x)/2

'except' works but I get 'unsupported operand type(s)' if i put in a number.

vishal
  • 47
  • 7

2 Answers2

2

You're trying to convert it to an int immediately. The try-except statement will check for errors, but only in the code that is contained in the try-except thingy. If you enter something wrong, the int conversion will immediately fail because the input is not an integer. Instead, put the int conversion (int(string)) into the try-except statement:

while True:
    x=input('enter no.->')


    try:
        x=int(x)
    except ValueError:
        print('try again')
    else:
        if (x/2)==1:
            break
print('program end')

The second one failed because you have to set x to the converted value, so you're basically trying to divide a string by 2.

As a note, I'm not sure how applicable this is, but my OOP professor has told me using an infinite loop and breaking/returning out of it is very bad programming practice, so you should just use a boolean value (while foo: ... ... ... foo = false). I'm not entirely sure why, as I haven't looked it up yet.

EDIT: Is it a bad practice to use break in a for loop?

In general, it's just how readable or error-prone you're willing to let it be.

Community
  • 1
  • 1
dma1324
  • 231
  • 1
  • 7
  • great explanation! very useful. Just 2 questions. Writing x=int(x) vs. x=int(x)/2 in 'TRY' block should work the same right because you can't divide string with 2 anyways so it will give a ValueError.? AND why do I have to set the value of x to int type? can't i just check with int(x)? – vishal Mar 20 '14 at 03:48
  • If x is not set to the int type, python treats it as a word. Imagine dividing a word by 2! How would that be done? The int(string) function converts a string to an integer. x=int(x)/2 in the TRY block should work the same, except if it works, your new x would already be divided by 2, so if you put x=int(x)/2 in the TRY block, you would have to remove the x/2 check in the if statement (instead: "if x==1:") because x has already been divided by 2. If it doesn't work, it will indeed give you a ValueError. – dma1324 Mar 20 '14 at 04:02
0

To loop while x is a number, you could do something like this (using Try and Except):

num = True
while num:
   x = raw_input("Enter a number: ")
   try:
       y = int(x)/2
       num = True
   except:
       num = False

print "Done!"
Luigi
  • 4,129
  • 6
  • 37
  • 57
  • @dma1234 is right about the coding practices. Also, you really don't need to worry about what the quotient is, as attempting to convert a non-numeric string to an integer will always give you an error. You could really even do away with the division if you're just looking for practice with try-except. – Luigi Mar 20 '14 at 03:45
  • I am trying to practice with complex statements (not that complex obviously, according to stack overflow standards) but still trying. – vishal Mar 20 '14 at 03:52