2016-06-25 50 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

不! '尝试:.......除了:继续'应该这样做。这取决于你如何编码。 –