我编写了一个可行的脚本,但是当我将其作为.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.')
答案 0 :(得分:1)
您可以轻松地将input()
放在脚本的末尾,以防止其终止。
正如您所知,input()
从标准输入中获取一个字符串,然后您可以随意使用它,在这种情况下不需要。