我的功能无法退出-如何强制退出?

时间:2018-10-02 18:43:19

标签: python python-3.x

import sys    
def menu():

    print("Welcome to Celebrity Dogs")

    print("Write A to Start Game or B to Quit.")

    answer = input()
    if answer == ("A"): 
        print("Let's play!")
    elif answer == ("B"):  
        print("Bye then!")
        sys.exit()
    else:
        print("invalid answer, select A or B")
        menu()


def numberofcards():

    number=int(input("Enter an even number that is less than 30 and more than 4."))
    if number < 30 and number > 4 and number % 2 == 0:
        print("Ok, Here are the cards.")
    else:
        print("Invalid")
        numberofcards()

嗨。我的代码运行正常,但是当我输入B退出时,它仍然要求我输入小于30且大于4的偶数。为什么? 在此先感谢x ps。 def ...,并且打印内容是代码xx的一部分

1 个答案:

答案 0 :(得分:0)

我会稍微修改您的代码以在正确的位置调用函数

def numberofcards():

    number=int(input("Enter an even number that is less than 30 and more than 4."))
    if number < 30 and number > 4 and number % 2 == 0:
        print("Ok, Here are the cards.")
    else:
        print("Invalid")
        numberofcards()


def menu():

    print("Welcome to Celebrity Dogs")
    print("Write A to Start Game or B to Quit.")

    answer = input()
    if answer == ("A"): 
        print("Let's play!")
        numberofcards()
    elif answer == ("B"):  
        print("Bye then!")
    else:
        print("invalid answer, select A or B")


menu()