【问题标题】:Stop .py files from terminating after script execution [duplicate]在脚本执行后停止.py文件终止[重复]
【发布时间】: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


【解决方案1】:

您可以轻松地将 input() 放在脚本末尾以防止脚本终止。

您可能知道,input() 从标准输入中获取一个字符串,然后您可以随心所欲地使用它,但在这种情况下是不必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多