简体   繁体   English

如何退出 if 语句和 go 回到开头

[英]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.我有几个 if 语句,但如果第一个输入为假,它仍然会 go 进入下一个 if 语句,而不是回到该 if 语句的开头并重新开始。

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

eg if weight inputed was 11 or height was greater then 80 it should always go back to the beginning and ask for weight.例如,如果输入的重量为 11 或身高大于 80,则应始终 go 回到开头并询问重量。 **If any condition is not met I need it to go back to the beginning and ask for the weight again. **如果不满足任何条件,我需要将它 go 回到开头并再次询问重量。 ** the program should restart and ask for the weight again. ** 程序应重新启动并再次询问重量。

Wrap all questions by one single while True loop instead.用一个 while True 循环代替所有问题。 In additional to break, you have to use continue in the condition you wants to restart the question from beginning.除了中断之外,您还必须在要从头开始重新开始问题的条件下使用continue

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM