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

Sam Ghanim
  • 17
  • 1
  • 1
  • 8
  • 2
    Your code indentation seems to be broken. – Moberg Aug 16 '17 at 13:58
  • 1
    See here on how to check if a variable contains a number: https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not – Moberg Aug 16 '17 at 14:00

4 Answers4

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.

RHSmith159
  • 1,823
  • 9
  • 16
  • but how can I make the program restart from the beginning when there's an error? – Sam Ghanim Aug 16 '17 at 15:27
  • In that case, you'll want a while loop for your input calls, I'll update my answer to give you an example – RHSmith159 Aug 17 '17 at 07:17
  • Thank you very much and I appreciate it :) – Sam Ghanim Aug 17 '17 at 07:54
  • Happy to help! :) – RHSmith159 Aug 17 '17 at 07:55
  • 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. – Sam Ghanim Aug 17 '17 at 08:04
  • AH! nice catch! Instead of casting the `inputted_` variables, I think the problem is actually the comparison, instead of comparing the inputted variables, the casted variables should be compared. This is because, `input()` returns a string, so for example comparing `inputted_age` to a number will throw an error. Thank you for pointing that out, I'll change that now. – RHSmith159 Aug 17 '17 at 08:47
0

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

Moberg
  • 5,253
  • 4
  • 38
  • 54
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)

GuangshengZuo
  • 4,447
  • 21
  • 27
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.

Ashish
  • 4,206
  • 16
  • 45