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

时间:2015-01-02 21:24:07

标签: 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()

1 个答案:

答案 0 :(得分:7)

input()生成字符串。您需要先将其转换为整数:

main_choice = int(input())

或者与字符串进行比较。

您可能希望查看Asking the user for input until they give a valid response以获取更多提示。