【发布时间】:2017-11-16 18:35:07
【问题描述】:
我编写了一个有效的脚本,但是当我将它作为 .py 文件电子邮件附件打开时,在我完成回答输入并运行后,它会关闭。我能做些什么来阻止这一切。如果有帮助,这里是代码:
# Write a program to solve an ancient Chinese puzzle: We count 35 heads
# and 94 legs among the chickens and rabbits on a farm. How many rabbits
# and how many chickens are there?
# Inputs the number of heads and legs, respectively.
# Makes sure it is read as an integer.
h=input('Enter the number of heads:')
heads=int(h)
le=input('Enter the number of legs:')
legs=int(le)
# Executes a while loop while the total number of rabbits is less
# than or equal to 35.
rabbits=0
while rabbits <= heads:
chickens = heads - rabbits
# Prints the result if there are 35 heads and 94 legs found.
if 2 * chickens + 4 * rabbits == legs and chickens + rabbits == heads:
print('There are', rabbits, 'rabbits and', chickens, 'chickens.')
break
rabbits+=1
if 2 * chickens + 4 * rabbits != legs or chickens + rabbits != heads:
print('No answer.')
【问题讨论】:
-
在末尾添加
input()。 -
使用
input("press return")
标签: python