Shimon Sofer
Shimon Sofer

Reputation: 35

how to exit an if statement and go back to the beginning

I have several if statements but if the first input is false it will still go on to the next if statement rather then going back to the beginning of that if statement and starting fresh.

the code below

 # check the weight of a single parcel
while True:
    pweight = float(input('How much does your parcel weigh in kg? '))
    if pweight > 10:
        print('Sorry the parcel is to heavy')
    elif pweight < 1:
        print('Sorry the parcel is to light')
    else:
        break
print('----------------------------------------------')
while True:
    height1 = float(input('Enter the parcel height in cm: '))
    if height1 > 80:
        print('Sorry, Your height is too large')
    else:
        break
print('----------------------------------------------')
while True:
    width1 = float(input('Enter the parcel width in cm: '))
    if width1 > 80:
        print('Sorry, Your width is too large')
    else:
        break
print('----------------------------------------------')
while True:
    length1 = float(input('Enter the parcel length in cm: '))
    if length1 > 80:
        print('Sorry, Your length is too large')
    else:
        break
print('----------------------------------------------')
while True:
    if (height1 + width1 + length1) > 200:
        print('Sorry, the sum of your dimensions are too large')
    else:
        print('Your parcels dimensions are', height1, 'x', width1, 'x', length1)
        print('Your parcel has been accepted for delivery, many thanks for using our service :)')
    break

e.g if weight inputed was 11 or height was greater then 80 it should always go back to the beginning and ask for weight. **If any condition is not met I need it to go back to the beginning and ask for the weight again. ** the program should restart and ask for the weight again.

Upvotes: 0

Views: 2163

Answers (1)

henrywongkk
henrywongkk

Reputation: 1918

Wrap all questions by one single while True loop instead. In additional to break, you have to use continue in the condition you wants to restart the question from beginning.

# check the weight of a single parcel
while True:
    pweight = float(input('How much does your parcel weigh in kg? '))
    if pweight > 10:
        print('Sorry the parcel is to heavy')
        continue
    elif pweight < 1:
        print('Sorry the parcel is to light')
        continue
    print('----------------------------------------------')
    height1 = float(input('Enter the parcel height in cm: '))
    if height1 > 80:
        print('Sorry, Your height is too large')
        continue
    print('----------------------------------------------')
    width1 = float(input('Enter the parcel width in cm: '))
    if width1 > 80:
        print('Sorry, Your width is too large')
        continue
    print('----------------------------------------------')
    length1 = float(input('Enter the parcel length in cm: '))
    if length1 > 80:
        print('Sorry, Your length is too large')
        continue
    print('----------------------------------------------')
    if (height1 + width1 + length1) > 200:
        print('Sorry, the sum of your dimensions are too large')
        continue
    else:
        print('Your parcels dimensions are', height1, 'x', width1, 'x', length1)
        print('Your parcel has been accepted for delivery, many thanks for using our service :)')
        break

Upvotes: 3

Related Questions