问题:我希望我的程序在运行后重新启动。意思是我想让它再次要求用户输入小时。我怎么做呢?我们还没有在课堂上讲过,我也没能在谷歌上找到。我觉得如果我去掉main()它会无限运行但是我必须在这里加上def ()
下面是我的代码:def main():
#Input
hour = int(input("Enter the hour: "))
if hour >= 1 and hour <= 12 : # Insures that the user inputs a number between "1" and "12"
suffix = input("Enter the suffix: ")
suffix = suffix.lower()
if suffix == "pm" and hour == 12: # Handles user input of "12pm"
hour = 12
print(hour)
elif suffix == "pm" and hour <= 12: # Handles all other "pm" user inputs
hour = hour + 12
print(hour)
elif suffix == "am" and hour == 12: # Handles user input of "12am"
print("0")
elif suffix == "am": # Handles all other "am" user inputs
print(hour)
else :
print("Error: The suffix must be am or pm.")
else :
print("Error: The hour must be between 1 and 12.")
main()
这有帮助吗?
while True:
main()
试试这个:
def main():
#Input
hour = int(input("Enter the hour: "))
if hour >= 1 and hour <= 12 : # Insures that the user inputs a number between "1" and "12"
suffix = input("Enter the suffix: ")
suffix = suffix.lower()
if suffix == "pm" and hour == 12: # Handles user input of "12pm"
hour = 12
print(hour)
elif suffix == "pm" and hour <= 12: # Handles all other "pm" user inputs
hour = hour + 12
print(hour)
elif suffix == "am" and hour == 12: # Handles user input of "12am"
print("0")
elif suffix == "am": # Handles all other "am" user inputs
print(hour)
else :
print("Error: The suffix must be am or pm.")
else :
print("Error: The hour must be between 1 and 12.")
while True:
main()
你可以使用while循环每次向用户询问小时数,并保持程序运行:
def main():
while(True):
#Input
hour = int(input("Enter the hour: "))
if hour >= 1 and hour <= 12 : # Insures that the user inputs a number between "1" and "12"
suffix = input("Enter the suffix: ")
suffix = suffix.lower()
if suffix == "pm" and hour == 12: # Handles user input of "12pm"
hour = 12
print(hour)
elif suffix == "pm" and hour <= 12: # Handles all other "pm" user inputs
hour = hour + 12
print(hour)
elif suffix == "am" and hour == 12: # Handles user input of "12am"
print("0")
elif suffix == "am": # Handles all other "am" user inputs
print(hour)
else :
print("Error: The suffix must be am or pm.")
else :
print("Error: The hour must be between 1 and 12.")
main()
一个解决方案是创建一个带有退出条件的循环,如下所示:
def main():
#Input
hour = int(input("Enter the hour: "))
if hour >= 1 and hour <= 12 : # Insures that the user inputs a number between "1" and "12"
suffix = input("Enter the suffix: ")
suffix = suffix.lower()
if suffix == "pm" and hour == 12: # Handles user input of "12pm"
hour = 12
print(hour)
elif suffix == "pm" and hour <= 12: # Handles all other "pm" user inputs
hour = hour + 12
print(hour)
elif suffix == "am" and hour == 12: # Handles user input of "12am"
print("0")
elif suffix == "am": # Handles all other "am" user inputs
print(hour)
else :
print("Error: The suffix must be am or pm.")
else :
print("Error: The hour must be between 1 and 12.")
def end():
inpt = input("Do you want to continue? (y/n)")
if(inpt == 'y'):
return True
else:
return False
run = True
while run:
main()
run = end()
这样每次运行时都会询问用户是否要继续。
另一种选择是,多次调用main()
。
至于你关于删除main()
调用的问题,在这种情况下,你的程序不会做任何事情。