Python If / Else Statement Assistance

时间:2015-04-21 20:54:35

标签: python

def main_menu():
    print ("Three Doors Down Figurative Language Game")
    print ("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
    print ("NOTE: TO SELECT, TYPE NUMBER OF OPTION")
    print ("")
    print (" 1) Begin Game")
    print ("")
    print (" 2) Options")
    print ("")
    print ("")
    menu_selection()
def menu_selection():
    valid_answer = ["1","2"]
    user_choice = str(input("Make a choice.."))
    if user_choice in valid_answer:
        def check_valid(user_choice):
            if user_choice == 1: #Error section V
                return("You started the game.")
            else:
                user_choice != 1
                return("Credits to ____, created by ____")
                check_valid(user_choice) #Error Section ^
    else:
        print("Please use an actual entry!")
        menu_selection()


def enterText():
    print("ENTER ANSWER!")

print (main_menu())

好的,所以应该标记错误。那个特定的if / else语句显示为“None”,我尝试了每种方法来修复它。一种方法适用于外部的if / else语句,但不适用于此。

1 个答案:

答案 0 :(得分:2)

您将输入作为字符串str(input())。然后,您要检查if user_input == 1;测试它是否是一个整数,即使它是一个字符串。相反,尝试使用int(input())转换为整数。此外,行user_input != 1是不必要的,它只相当于在代码中编写True。此外,您在if语句中定义了一个函数,该函数不应该存在:

def main_menu():
    print ("Three Doors Down Figurative Language Game")
    print ("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
    print ("NOTE: TO SELECT, TYPE NUMBER OF OPTION")
    print ("")
    print (" 1) Begin Game")
    print ("")
    print (" 2) Options")
    print ("")
    print ("")
    menu_selection()
def menu_selection():
    valid_answer = ["1","2"]
    user_choice = int(input("Make a choice.."))
    if user_choice in valid_answer:
        if user_choice == 1:
            return("You started the game.")
        else:
            return("Credits to ____, created by ____")
            check_valid(user_choice)
    else:
        print("Please use an actual entry!")
        menu_selection()


def enterText():
    print("ENTER ANSWER!")

print (main_menu())