lundi 27 juin 2016

KeyError issue with dictionaries

Im using a dictionary in my code and comparing the user's input to it, but when the user input nothing and just hits enter the program falls over and gives me a key error. I tried to use 'Try and Except' but that way the the program loops to the beginning which i dont want, i want to keep the program in the sub loop until the user inputs something that is within the boundary.

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.")

This is My Text file that i converted into a dictionary which is called 'dtablet'.

modelquestion:What is the Model of your Tablet?

namequestion:What is the Name of your Tablet?

storagequestion:What is the STORAGE of your Tablet?

problemquestion:What is the PROBLEM with your Tablet?

phonemodels:apple,samsung,sony

apple:IPAD1,IPAD2,IPAD3

samsung:TAB1,TAB2,TAB3

sony: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:cracked,broken

32GB:cracked,broken

64GB:cracked,broken

CRACKED:Problem = Cracked Solution = Take your phone to the repair shop

BROKEN:Problem = Broken Solution = Take your phone to the repair shop

Aucun commentaire:

Enregistrer un commentaire