【问题标题】:Making a program than would ask the user for an integer then will asked if they wish to loop or quit当输入输入 y 时如何循环这个
【发布时间】:2022-01-20 11:34:45
【问题描述】:
def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
            num_two = int(input('Do you want to play again n/Y:'))
       
        except:
            if num_input == "y":
                continue
        finally:
            print("Goodbye")
        print(num_two)

【问题讨论】:

  • num_two 根本不会是一个数字。

标签: python if-statement try-catch except finally


【解决方案1】:

在再见后使用休息声明:

def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
            num_two = int(input('Do you want to play again n/Y:'))
       
        except:
            if num_input == "y":
                continue
        finally:
            print("Goodbye")
            break
        print(num_two)

【讨论】:

    【解决方案2】:

    重播提示要求输入一个字符串,而不是数字。不要试图把它当作一个数字。将try 语句限制为测试数字输入:如果得到ValueError,请立即重新启动循环,而不是尝试测试它是偶数还是奇数。

    当您获得再次播放的响应时,如果响应不是y,请使用break 退出循环。

    循环退出后打印“再见”。

    def interact():
        while True:
            try:
                num = int(input("Please input an integer: "))
            except ValueError:
                continue  # Ask for a number again
    
            if num % 2 == 0:
                print("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
    
            response = input("Play again? (n/Y) ")
            if response != "y":
                break
        print("Good bye")
    

    【讨论】:

    • 非常感谢。我只是 python 的新手。感谢您提供新信息。它会很有帮助
    【解决方案3】:

    我不完全明白你在追求什么,但也许可以试试这样:

    def interact():
        while True: 
            try:
                num = int(input("Please input an integer: "))
                if (num % 2) == 0:
                    print ("{0} is even".format(num))
                else:
                    print("{0} is odd".format(num))
            except:
                continue
            if input("Would you like to play again? Y/N: ").lower() == "y":
                continue
            else:
                print("goodbye")
                break
    

    我相信这会产生你所追求的效果。

    【讨论】:

      【解决方案4】:

      我认为代码应该是这样的:

      def interact():
        try:
            while True:
                num = int(input("Please input an integer: "))
                if (num % 2) == 0:
                    print ("{0} is even".format(num))
                else:
                    print("{0} is odd".format(num))
                num_two = str(input('Do you want to play again n/Y:'))
      
                if num_two != "y":
                    raise
        except:
            print("Goodbye")
      

      【讨论】:

        【解决方案5】:

        更紧凑的方法:

        def interact():
            want_continue = "Y"
            while want_continue == "Y":
                try:
                    num = int(input("Please input an integer: "))
                    print ("{0} is {1}".format(num, "even" if num % 2 == 0 else "odd"))
                    want_continue = input('Do you want to play again (n/Y):').upper()
                except ValueError:
                    print("Please enter a number")
            print("Goodbye")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-19
          • 2020-02-23
          • 2022-11-30
          • 2023-03-16
          • 1970-01-01
          • 2021-07-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多