如何退出 if 语句并返回开头
how to exit an if statement and go back to the beginning
我有几个 if 语句,但如果第一个输入为假,它仍会继续执行下一个 if 语句,而不是返回到该 if 语句的开头并重新开始。
下面的代码
# 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
例如,如果输入的体重是 11 或身高大于 80,它应该总是回到开头并询问体重。
**如果不满足任何条件,我需要它回到开始并再次询问重量。 **
程序应该重新启动并再次询问重量。
将所有问题换成一个 while True 循环。除了中断之外,您还必须在要从头开始重新开始问题的条件下使用 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
我有几个 if 语句,但如果第一个输入为假,它仍会继续执行下一个 if 语句,而不是返回到该 if 语句的开头并重新开始。
下面的代码
# 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
例如,如果输入的体重是 11 或身高大于 80,它应该总是回到开头并询问体重。 **如果不满足任何条件,我需要它回到开始并再次询问重量。 ** 程序应该重新启动并再次询问重量。
将所有问题换成一个 while True 循环。除了中断之外,您还必须在要从头开始重新开始问题的条件下使用 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