2016-06-25 200 views
0

在我的代碼中使用字典並比較用戶的輸入,但是當用戶輸入任何內容並且只是輸入時,輸入程序就會結束並給我一個關鍵錯誤。我嘗試使用'Try和Except',但這樣程序循環到我不想要的開始處,我希望將程序保留在子循環中,直到用戶輸入邊界內的某些東西。字典中的KeyError問題

ftablet=open("tablet.txt").readlines() devicelist=["phone","tablet"] 

dtablet = {} 

for line in ftablet: 
     (key,val) = line.split(":") 
     dtablet[str(key)] = val.strip(",")  

while True:  
    counter=0  
    device="" 

    while device not in (devicelist): 
     device=input(What is your device?: ").lower().replace(" ","") 
     print() 


    if device == "tablet": 
     usermodel="" 

     while usermodel != (dtablet["phonemodels"]): 
     print(dtablet["modelquestion"]) 
     usermodel=input("Enter MODEL Here: ").lower().replace(" ","") 

     if usermodel in (dtablet["phonemodels"]): 
      name="" 

      while name != (dtablet[usermodel]): 
       print(dtablet["namequestion"]) 
       print("Enter any of these : ",(dtablet[model])) 
       name=input("Enter NAME Here: ").upper().replace(" ","") 

       if name in (dtablet[usermodel]): 
        storage="" 

        while storage != (dtablet[name]): 
        print(dtablet["storagequestion"]) 
        print("Enter any of these : ",(dtablet[name])) 
        storage=input("Enter STORAGE Here: ").upper().replace(" ","") 

        if storage in (dtablet[name]): 
         problem=input("What is the PROBLEM with your tablet?\nEnter Here: ").upper() 
         print() 

         for word in problem.split(): 
          if word in (dtablet[storage]): 
           print(dtablet[word]) 
           print("Thanks for using my troubleshooting program") 
           counter=1 
         if counter==0: 
          print("We dont have your keyword in out database") 
    else: 
     print("Sorry! We do not have that device in out database please choose from the ones listed above.") 

這是我的文本文件,我轉換成名爲'dtablet'的字典。

modelquestion:什麼是您的平板電腦的型號?

namequestion:什麼是您的平板電腦的名稱?

storagequestion:什麼是您的平板電腦的存儲?

problemquestion:你的平板電腦有什麼問題?

phonemodels:蘋果,三星,索尼

蘋果:IPAD1,IPAD2,IPAD3

三星:TAB1,TAB2,TAB3

索尼:XPERIAZ2,XPERIAZ3,XPERIAZ4

IPAD1: 16GB,32GB,64GB

IPAD2:16GB,32GB,64GB

IPAD3:16GB,32GB,64GB

TAB1:16GB,32GB,64GB

TAB2:16GB,32GB,64GB

TAB3:16GB,32GB,64GB

XPERIAZ1:16GB,32GB ,64GB

XPERIAZ2:16GB,32GB,64GB

XPERIAZ3:16GB,32GB,64GB

16GB:破裂,斷裂

32GB:破裂,斷裂

64GB:破裂,斷裂

破碎:問題=破裂的解決方案=帶上手機到維修 店

BROKEN:問題=破碎的解決方案=把你的手機送到維修店

+0

提供整個回溯可能會有幫助。 – Arpan

+1

@Arpan突出顯示僅在問題標記爲[tag:python]標記時才起作用。有一個掛起的編輯添加它。 –

+0

你確定有一個關鍵''模型''如果你打印出'dtable.keys()'會怎麼樣? –

回答

0

你的問題是,你沒有在鑰匙周圍加引號。這意味着它無論如何不會找到它。

另外,你爲什麼不把它保持在一個循環?它會更有效率。

+0

但如果我將它保存在一個循環中,我將如何驗證輸入? –

0

,你既有

dtablet[model] # the key is the model variable 

dtablet["model"] # the key is the "model" string 
在你的代碼

,這是故意的嗎?

如果用戶按下「輸入」,然後將model是空字符串,並且這種表達:

model in dtablet[model] 

總是爲真,如果dtablet [模型]是一個字符串。

+0

是的,這是故意的,因爲「模型」是一個關鍵,我知道模型將由用戶輸入分配的位置。 –

0

首先,您的字典中沒有「模型」鍵。您可以打印出dtablet.keys()並查看是否有「模型」。如果沒有,那麼dtablet [「model」]將100%返回一個KeyError。

其次,當你鍵入while model != (dtablet["model"]):,因爲model是一個空字符串,如果dtablet [「模型」]是一個字符串,那麼它不會去while循環中,因爲一個空字符串始終是任何字符串的子串。

+0

我添加了我的字典和我的整個代碼,這將使它更容易理解。 –

+0

我可以清楚地看到你的txt文件左側沒有「模型」,所以dtablet [「model」]不存在。 –

+0

我更改了變量名稱和鍵名,以便更容易理解 –

0

你知道while迴路有break選項和continue選項嗎?
如果您「希望讓程序保持在子循環中,直到用戶輸入邊界內的東西」測試,並且如果輸入不是您要查找的內容,則爲continue

+0

所以我應該在每個while循環之後繼續嗎? –

+0

不! '嘗試:.......除了:繼續'應該這樣做。這取決於你如何編碼。 –