停止一个程序但不退出它?

时间:2016-09-07 16:55:05

标签: python-3.x

我正在尝试用if else语句为学校编写一个简单的程序。我希望脚本在第一个问题之后停止,如果它不符合标准,则不关闭程序。以下是我到目前为止的情况:

age=int(input("How old are you? ")) #add a line to skip the other two inputs if not old enough
#registered = input("Are you a registered voter? yes/no ").lower()[:1]
#precinct   = input("Are you in your registered precinct? yes/no ").lower()[:1]

if age >= 18:
    print("You are old enough to vote. ") #Determines if old enough to vote.

else:
    print("You are not old enough to vote. ")


registered = input("Are you a registered voter? yes/no ").lower()[:1]

if registered == 'y':
    print("You are registered to vote. ")

else:
    print("Do you have documentation showing your permanent address? ")

precinct   = input("Are you in your registered precinct? yes/no ").lower()[:1]

if precinct == 'y':
    print("You are in your precinct and can vote. ")

else:
    print("You must be in your precinct to be able to vote. ")


if age >= 18 and registered == 'y' and precinct == 'y':
    print("Congratulations you may vote! ")

else:
    print("Sorry you may not vote. ")

1 个答案:

答案 0 :(得分:0)

您可以添加空input()以使程序暂停而不退出。它需要按Enter键才能推进程序。

age = int(input("How old are you? "))

if age >= 18:
    print("You are old enough to vote. ") #Determines if old enough to vote.
else:
    print("You are not old enough to vote. ")
    input()  # Program is paused until enter is pressed