2017-09-14 56 views
-1
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語句,請幫忙!爲什麼我的if/elif/else不工作?

+0

因爲你轉換爲大寫,你可以做'如果答案=='A「':那會很好的... –

+0

@ Jean-FrançoisFabre難道你沒有這個笨蛋嗎? –

+0

是的,但問題被標記爲pytho 3.x只。我編輯爲python。我希望我有一個python 3.x標籤dupehammer! –

回答

-2

它應該是elif answer == "P" or answer == "p",你需要兩個answer ==的條件。

/E您的第一個if是說(If answer equals "A" is true) or ("a" is true)和「a」的計算結果爲真實的,因爲它是非零的,所以它使用的是第一if聲明,即使你打「P」

+0

我嘗試了你的建議,但我不斷收到此錯誤消息Traceback(最近調用最後一次): File如果answer.upper()==「A」: AttributeError:'builtin_function_or_method'對象沒有屬性'upper',那麼「F:/ Yr 11/CT/CSV/asdf.py」,第9行,