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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)?
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.
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!"

2 Comments

@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.
I am trying to practice with complex statements (not that complex obviously, according to stack overflow standards) but still trying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.