Python单位转换实验:如何实现无效输入超3次时程序逻辑终止
问题分析
- 原代码触发终止条件后没有立即中断当前流程,会继续执行后续其他转换的输入逻辑,无法立刻终止程序
- 温度输入的校验阈值误写为1000,不符合需求要求的大于100即判定为输入错误的规则
- 程序终止提示放在了循环外的初始化阶段,永远不会被执行,无法正确提示用户
修复要点
- 每个输入模块触发终止条件时,设置终止标记后立刻加
break跳出当前循环,外层主循环检测到终止标记就会停止整个程序 - 将温度输入的校验阈值从1000调整为100,匹配需求
- 将程序终止提示移到触发终止的分支内,保证触发时正常输出
- 完全保留原有的函数名、计算公式、基础交互逻辑不变
修改后完整代码
def main(): terminate=False while terminate==False: miles_tries=0 mile_value=float(input('Roger, Enter a number of miles you wish to convert: ')) while miles_tries <2 and mile_value<0: print('Roger, the value of the miles cannot be a negative number') mile_value=float(input('Roger, Enter a number of miles you wish to convert: ')) miles_tries+=1 if miles_tries >=2 and mile_value<0: print('Program has been terminated\n') terminate=True break else: milesToKm(mile_value) temp_tries=0 temp_value=float(input('Roger, Enter a temperature you wish to convert: ')) while temp_tries <2 and temp_value>100: print('Roger, the value of the temperature cannot be greater than 100') temp_value=float(input('Roger, Enter a number of degrees fahrenheit you wish to convert: ')) temp_tries+=1 if temp_tries >=2 and temp_value>100: print('Program has been terminated\n') terminate=True break else: FahToCel(temp_value) gallon_tries=0 gallon_value=float(input('Roger, Enter a number of gallons you wish to convert: ')) while gallon_tries <2 and gallon_value<0: print('Roger, the value of the gallons cannot be a negative number') gallon_value=float(input('Roger, Enter a number of gallons you wish to convert: ')) gallon_tries+=1 if gallon_tries >=2 and gallon_value<0: print('Program has been terminated\n') terminate=True break else: GalToLit(gallon_value) pound_tries=0 pound_value=float(input('Roger, Enter a number of pounds you wish to convert: ')) while pound_tries <2 and pound_value<0: print('Roger, the value of the pounds cannot be a negative number') pound_value=float(input('Roger, Enter a number of pounds you wish to convert: ')) pound_tries+=1 if pound_tries >=2 and pound_value<0: print('Program has been terminated\n') terminate=True break else: PoundToKg(pound_value) inches_tries=0 inches_value=float(input('Roger, Enter a number of inches you wish to convert: ')) while inches_tries <2 and inches_value<0: print('Roger, the value of the inches cannot be a negative number') inches_value=float(input('Roger, Enter a number of inches you wish to convert: ')) inches_tries+=1 if inches_tries >=2 and inches_value<0: print('Program has been terminated\n') terminate=True break else: InchesToCm(inches_value) def milesToKm(value): km=int(value)*1.6 print("Roger,", value, " miles is equal to ", km, " kilometers\n") def FahToCel(value): c=(int(value)-32)*5/9 print("Roger, ",value, " degrees fahrenheit is equal to ", c, " degrees celsius.\n") def GalToLit(value): gal=int(value)*3.9 print("Roger, ",value, " gallons is equal to ", gal, " liters.\n") def PoundToKg(value): kg=int(value)*.45 print("Roger, ",value, " Pounds is equal to ", kg, " kilograms\n") def InchesToCm(value): cm=int(value)*2.54 print("Roger, ",value, " inches is equal to ", cm, " centimeters\n") main() print('Have a good day')
逻辑说明
当前重试逻辑中,首次输入算第1次,允许最多2次重试,合计3次输入机会,符合“输入错误超过3次终止程序”的要求,任意一个输入项触发终止条件后都会立刻结束整个程序,不会继续执行后续步骤。
内容的提问来源于stack exchange,提问作者Ayman Afeef
相关产品推荐
相关产品推荐

