while True 和 if 语句打印相同的计算
while True and if statement printing the same calculation
我正在开发一个英尺到英寸的转换器,它实际上非常容易创建,就像我以前做过的那样。但是这次,我需要使用 while 循环。基本上,如果脚数等于 0,它应该打印 'the program has finished successfully.' 如果脚数大于或等于 1,代码将继续计算,直到用户输入 0 退出脚本。我的问题是,当我测试退出脚本时,它会打印:
“0 英尺 = 0 英寸
该程序已成功完成。”
我只希望它打印终止消息,而不是 0 英尺 = 0 英寸。我不知道我做错了什么。
def feet_to_inches(feet):
return 12 * feet
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
print(f'{feet} feet = {inches} inches')
while True:
if feet == 0:
print('The program has finished successfully.')
break
...
if feet >= 1:
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
print(f'{feet} feet = {inches} inches')
continue
我已经为需要的更改添加了评论
几件事:
- 缩进错误(可能是 Whosebug 的问题)
- 由于您将需要用户输入来决定是否退出循环,因此您可以在循环中请求输入,然后再决定要做什么
- 最后不需要继续
建议代码:
def feet_to_inches(feet):
return 12 * feet #indentation error
while True:
feet = int(input('Enter the number of feet (or 0 to end): ')) #moving input inside the while loop
inches = feet_to_inches(feet)
if feet == 0:
print('The program has finished successfully.')
break
elif feet >= 1:
# feet = int(input('Enter the number of feet (or 0 to end): '))#not required
# inches = feet_to_inches(feet) #not required
print(f'{feet} feet = {inches} inches')
# continue #this is not required
输出:
Enter the number of feet (or 0 to end): 1
1 feet = 12 inches
Enter the number of feet (or 0 to end): 2
2 feet = 24 inches
Enter the number of feet (or 0 to end): 3
3 feet = 36 inches
Enter the number of feet (or 0 to end): 0
The program has finished successfully.
请参阅下面的代码,它也检查默认大小写。
def feet_to_inches(feet):
return 12 * feet # Please be aware your indentation error
while True:
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
if feet == 0:
print('The program has finished successfully.')
break
elif feet >= 1:
print(f'{feet} feet = {inches} inches')
else:
print(f'Please provide a positive value')
示例输出:
Enter the number of feet (or 0 to end): 2
2 feet = 24 inches
Enter the number of feet (or 0 to end): 1
1 feet = 12 inches
Enter the number of feet (or 0 to end): -8
Please provide a positive value
Enter the number of feet (or 0 to end): 23
23 feet = 276 inches
Enter the number of feet (or 0 to end): 0
The program has finished successfully.
这是 feet-to-inches 转换器的备用代码结构。
while True:
feet = int(input('Please enter the number of feet (or 0 to end): '))
if feet == 0:
print('Program ended successfully.')
break
elif feet < 0:
print('Please enter a positive value.')
continue
else:
feet_to_inches = 12 * feet
print('%d feet converts to %d inches'%(feet, feet_to_inches))
我正在开发一个英尺到英寸的转换器,它实际上非常容易创建,就像我以前做过的那样。但是这次,我需要使用 while 循环。基本上,如果脚数等于 0,它应该打印 'the program has finished successfully.' 如果脚数大于或等于 1,代码将继续计算,直到用户输入 0 退出脚本。我的问题是,当我测试退出脚本时,它会打印: “0 英尺 = 0 英寸 该程序已成功完成。” 我只希望它打印终止消息,而不是 0 英尺 = 0 英寸。我不知道我做错了什么。
def feet_to_inches(feet):
return 12 * feet
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
print(f'{feet} feet = {inches} inches')
while True:
if feet == 0:
print('The program has finished successfully.')
break
...
if feet >= 1:
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
print(f'{feet} feet = {inches} inches')
continue
我已经为需要的更改添加了评论
几件事:
- 缩进错误(可能是 Whosebug 的问题)
- 由于您将需要用户输入来决定是否退出循环,因此您可以在循环中请求输入,然后再决定要做什么
- 最后不需要继续
建议代码:
def feet_to_inches(feet):
return 12 * feet #indentation error
while True:
feet = int(input('Enter the number of feet (or 0 to end): ')) #moving input inside the while loop
inches = feet_to_inches(feet)
if feet == 0:
print('The program has finished successfully.')
break
elif feet >= 1:
# feet = int(input('Enter the number of feet (or 0 to end): '))#not required
# inches = feet_to_inches(feet) #not required
print(f'{feet} feet = {inches} inches')
# continue #this is not required
输出:
Enter the number of feet (or 0 to end): 1
1 feet = 12 inches
Enter the number of feet (or 0 to end): 2
2 feet = 24 inches
Enter the number of feet (or 0 to end): 3
3 feet = 36 inches
Enter the number of feet (or 0 to end): 0
The program has finished successfully.
请参阅下面的代码,它也检查默认大小写。
def feet_to_inches(feet):
return 12 * feet # Please be aware your indentation error
while True:
feet = int(input('Enter the number of feet (or 0 to end): '))
inches = feet_to_inches(feet)
if feet == 0:
print('The program has finished successfully.')
break
elif feet >= 1:
print(f'{feet} feet = {inches} inches')
else:
print(f'Please provide a positive value')
示例输出:
Enter the number of feet (or 0 to end): 2
2 feet = 24 inches
Enter the number of feet (or 0 to end): 1
1 feet = 12 inches
Enter the number of feet (or 0 to end): -8
Please provide a positive value
Enter the number of feet (or 0 to end): 23
23 feet = 276 inches
Enter the number of feet (or 0 to end): 0
The program has finished successfully.
这是 feet-to-inches 转换器的备用代码结构。
while True:
feet = int(input('Please enter the number of feet (or 0 to end): '))
if feet == 0:
print('Program ended successfully.')
break
elif feet < 0:
print('Please enter a positive value.')
continue
else:
feet_to_inches = 12 * feet
print('%d feet converts to %d inches'%(feet, feet_to_inches))