Python 而True和if语句打印相同的计算
我正在开发一个英尺到英寸的转换器,它实际上非常容易创建,就像我以前做的那样。但这一次,我需要使用while循环。基本上,如果英尺数等于0,它应该打印“程序已成功完成”。如果英尺数大于或等于1,代码将继续进行计算,直到用户输入0退出脚本。我的问题是,当我测试退出脚本时,它会打印: “0英尺=0英寸 程序已成功完成。” 我只想让它打印终止消息,而不是0英尺=0英寸。我不知道我做错了什么Python 而True和if语句打印相同的计算,python,if-statement,while-loop,python-3.8,converters,Python,If Statement,While Loop,Python 3.8,Converters,我正在开发一个英尺到英寸的转换器,它实际上非常容易创建,就像我以前做的那样。但这一次,我需要使用while循环。基本上,如果英尺数等于0,它应该打印“程序已成功完成”。如果英尺数大于或等于1,代码将继续进行计算,直到用户输入0退出脚本。我的问题是,当我测试退出脚本时,它会打印: “0英尺=0英寸 程序已成功完成。” 我只想让它打印终止消息,而不是0英尺=0英寸。我不知道我做错了什么 def feet_to_inches(feet): return 12 * feet feet = int(in
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
我已经为所需的更改添加了注释 几件事:
- 缩进错误(可能是堆栈溢出)
- 因为您需要用户输入来决定是否退出循环,所以您可以在循环中请求输入,然后决定做什么
- 最终不需要继续
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.
这是英尺到英寸转换器的另一种代码结构
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))
为True时:
英尺=int(输入('请输入英尺数(或0到结尾):'))
如果英尺=0:
打印('程序已成功结束')
打破
elif英尺<0:
打印('请输入一个正值')
持续
其他:
英尺到英寸=12*英尺
打印(“%d英尺转换为%d英寸“%(英尺,英尺到英寸))
你总是接受输入,进行转换,打印转换,然后点击条件结束程序……这真的很有帮助!我终于把它打印正确了,谢谢你对我的问题的意见!