Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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?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.

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.

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.

added 82 characters in body
Source Link
dma1324
  • 231
  • 1
  • 7

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.

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?

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.

Source Link
dma1324
  • 231
  • 1
  • 7

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?