0

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

2
  • 2
    Your code indentation seems to be broken. Commented Aug 16, 2017 at 13:58
  • 1
    See here on how to check if a variable contains a number: stackoverflow.com/questions/3501382/… Commented Aug 16, 2017 at 14:00

4 Answers 4

1

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.

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

6 Comments

but how can I make the program restart from the beginning when there's an error?
In that case, you'll want a while loop for your input calls, I'll update my answer to give you an example
Thank you very much and I appreciate it :)
Happy to help! :)
There's a problem I found in lines 37 and 40 TypeError: unorderable types: str() >= int() So I fixed that by adding int() to the inputted_age and inputted_height so it became like int(inputted_age) and int(inputted_height) Just if you can fix it on the comment for other viewers.
|
0

The break statement breaks out of a loop (for-loop or while-loop). Outside of this context it does not make sense.

Comments

0

break cannot restart your program, break can only be used in loop such as for or while.

In you case, just use exit(-1)

Comments

0

there is no loop in your program. break can't be used outside a loop. You can use sys.exit() instead of breakand pass instead of continue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.