我的if语句/调用问题有问题,请帮助:)

-4 python

我的代码有一些问题,看看main()部分,我输入选择2,它调用open_file.但它需要1作为别的而不是IF ...而当我用1 agin写它只是在屏幕上打印1,我做错了什么?Python版本3.4.2.

import sys
name=input("What is your name? : ")
print ("Welcome " + name)


def main():
    print ("What do you want to do")
    print ("1) Open A File")
    print ("2) open Web Browser")
    print ("3) Exit OS")

    main_choice=input()

    if main_choice == 1:
        open_file()
    elif main_choice == 2:
        web_browser()
    elif main_choice == 3:
        exit_os()
    else:
        unknown_number()


def unknown_number():
    print ("The choice you made does not exist, please choose a valid option")
    main


def open_file():
    print ("What do you want to do?")
    print ("1) Open A File TXT FILES ONLY")
    print ("2) Back to main menu")

    open_file_choice=input()

    if open_file_choice == 1:
        open_file_confirm()
    elif open_file_choice == 2:
        main()
    else:
        unknown_number()

def open_file_confirm():
    file=open("","r")
    print ("What is the file name? Include extension")
    file=input()

main()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

input()产生一个字符串.您需要先将其转换为整数:

main_choice = int(input())
Run Code Online (Sandbox Code Playgroud)

或者与字符串进行比较.

您可能希望查看请求用户输入,直到他们提供有效响应以获取更多提示.