Sam Ghanim Sam Ghanim - 1 year ago 149
Python Question

python "break" error: break outside loop

When I was developing my first code I faced a problem which with the

break
command which I tried to use to restart the program if there's an error.

Take a look on the code maybe you would understand better.

Name = str(input("Please enter Your Name:"))
Age = input("Please enter your age: ")
if Age != int():
print ("Error! Check the age")
break
elif Age == int():
continue
Height = input("Please enter your height: ")
if Height != int():
print ("Error! Check the Height")
break
elif Height == int():
continue

if Age == int() and Age >= 18 and Height == int() and Height >= 148:
print("You're able to drive a car " + (Name) )

elif Age == int() and Age < 18 and Height == int() and Height > 148:
print("You're not able to drive a car " + (Name) )

elif Age and Height != int() :
print ("Error! , Age or Height are not numbers")


error:


"C:\Users\Ghanim\Desktop\Coding\Documents\Projects\Python\Project1\Project1.py", line 6
break
^
SyntaxError: 'break'

outside loop

Answer Source

The break statement is used to exit loops, not the program. Use sys.exit() to quit the program, you'll need to import sys too.

EDIT:

In answer to your comment, this is how I'd probably do it:

while True:

    inputted_name = input("Please enter your name:")

    try:
        name = str(inputted_name)
    except ValueError:
        print("Please enter a valid name")
    else:
        break


while True:

    inputted_age = input("Please enter your age:")

    try:
        age = int(inputted_age)
    except ValueError:
        print("Please enter a valid age")
    else:
        break


while True:

    inputted_height = input("Please enter your height:")

    try:
        height = float(inputted_height)
    except ValueError:
        print("Please enter a valid height")
    else:
        break


if age >= 18 and height >= 148:
    print("You're able to drive a car {}".format(inputted_name))

if age < 18 and height > 148:
    print("You're not able to drive a car {}".format(inputted_name))

So there are a few changes:

Each stage of the user input is in its own loop. I've used try/except/else statements which try to cast the input to the correct type, except ValueErrors (thrown if it cant be cast, which will happen if the user puts a text answer to the input age for example. If it casts to the correct type successfully, the loop is broken and the script moves onto the next one. Having separate loops for each one means if the user puts in an incorrect value for one of them, they don't have to redo the whole thing.

I've also used format() to insert name into the final strings to avoid having to do string concatenation.

Also, just a quick note, I'm assuming you're using Python 3 for this. However, if you're using Python 2 input() should be replaced with raw_input(). In Python 2 input() will try and evaluate the user input as an expression, whereas raw_input() will return a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download