【问题标题】:Ask the user if they want to repeat the same task again询问用户是否想再次重复相同的任务
【发布时间】:2013-10-09 21:38:21
【问题描述】:

如果用户完成程序,我希望他们收到一个问题,询问他们是否想再试一次。如果他们回答是,我想重新运行程序。

import random
print("The purpose of this exercise is to enter a number of coin values") 
print("that add up to a displayed target value.\n") 
print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.") 
print("Hit return after the last entered coin value.")
print("--------------------") 
total = 0 
final_coin = random.randint(1, 99)
print("Enter coins that add up to", final_coin, "cents, on per line") 
user_input = int(input("Enter first coin: "))
total = total + user_input

if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25:
   print("invalid input")

while total != final_coin:
    user_input = int(input("Enter next coin: "))
    total = total + user_input

if total > final_coin:
    print("Sorry - total amount exceeds", (final_coin)) 

if total < final_coin:
    print("Sorry - you only entered",(total))

if total== final_coin: 
    print("correct")

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以将整个程序包含在另一个 while 循环中,询问用户是否要重试。

    while True:
      # your entire program goes here
    
      try_again = int(input("Press 1 to try again, 0 to exit. "))
      if try_again == 0:
          break # break out of the outer while loop
    

    【讨论】:

    • 你不能直接在while语句中检查而不是在if语句中。你可以写while try_again != 0,然后是整个代码。作为 while 中的最后一条语句,您可以执行 try_again = int(input(....))
    【解决方案2】:

    这是对已接受答案的增量改进:

    按原样使用,任何来自用户的无效输入(例如空 str,或字母“g”等)都会在调用 int() 函数时引发异常。

    此类问题的一个简单解决方案是使用 try/except-尝试执行任务/代码,如果它有效 - 很好,否则(除了这里就像 else:) 做其他事情。

    在可能尝试的三种方法中,我认为下面的第一种是最简单的,不会使您的程序崩溃。

    选项1:只需使用一个选项输入的字符串值再去一次

    while True:
        # your entire program goes here
        
        try_again = input("Press 1 to try again, any other key to exit. ")
        if try_again != "1":
            break # break out of the outer while loop
    

    选项 2:如果使用 int(),防止错误的用户输入

    while True:
        # your entire program goes here
    
        try_again = input("Press 1 to try again, 0 to exit. ")
        try:
            try_again = int(try_again)  # non-numeric input from user could otherwise crash at this point
            if try_again == 0:
                break # break out of this while loop
        except:
            print("Non number entered")
        
    

    选项 3:循环直到用户输入两个有效选项之一

    while True:
        # your entire program goes here
        
        try_again = ""
        # Loop until users opts to go again or quit
        while (try_again != "1") or (try_again != "0"):
            try_again = input("Press 1 to try again, 0 to exit. ")
            if try_again in ["1", "0"]:
                continue  # a valid entry found
            else:
                print("Invalid input- Press 1 to try again, 0 to exit.")
        # at this point, try_again must be "0" or "1"
        if try_again == "0":
            break
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 2020-02-15
      • 2016-09-22
      • 2017-01-30
      • 1970-01-01
      • 2021-04-06
      • 2021-06-11
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多