为什么我的if / elif / else无法正常工作?

时间:2017-09-14 19:09:59

标签: python python-3.x

import CSV

file_name = input("PLease type in a file name including the extension e.g. .docx or .txt\nelse:")
start = 0
error = 0
with open(file_name, newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    answer = input("Do you want all information to be displayed or only part of it?(A/P)")
    answer = answer.upper()
    print(answer)

    if answer == "A" or "a":
        All_Info()
    elif answer == "P" or "p":
        Part_Info()
    else:
        if error < 2:
            error = error + 1
            answer = input("Please only type in A or P")

当我尝试运行此程序并在输入csv文件名后按p键时,它会忽略elif并直接转到if语句,请帮忙!

1 个答案:

答案 0 :(得分:-2)

应该是elif answer == "P" or answer == "p",您的条件中需要两个answer ==

/ e您的第一个if正在说(If answer equals "A" is true) or ("a" is true)和&#34; a&#34;计算结果为true,因为它非零,所以即使你点击了&#34; p&#34;

,它也会使用第一个if语句