How can i put pickle in this code


#1

Is there any way of putting pickle in this code?

while True:
    import time
    print("This is a yes or no troubleshooting question")
    time.sleep(0.75)
    user_input=input("What is the brand of your device? ")
    if user_input in("iPhone", "HTC", "Samsung", "Nokia", "iphone", "samsung", "htc", "nokia", "Apple", "apple", "Sony", "sony", "Huawei", "huawei", "LG", "lg"):
        time.sleep(0.75)
        print("Next question")
    else:
        print("Sorry I dont know that brand")
        break    
    import time
    time.sleep(0.75)
    varSentence =  input("Explain what is wrong with the phone: ")
    varSplit = varSentence.split()
    if "display" in varSplit: #This will detect that there is a keyword 
        time.sleep(0.75)
        print ("If the display isn't working, please take it to the nearest repair shop.") #This is the answer to the keyword which you typed in
    elif "water" in varSplit:
        time.sleep(0.75)
        print ("If some liquid has entered the phone, you will need to put it in a bag of rice for the liquid to be absorbed.")
    elif "audio" in varSplit:
        time.sleep(0.75)
        print ("If the phone has audio problems, you will need to it to the nearest repair shop.")
    elif "battery" in varSplit:
        time.sleep(0.75)
        print ("If you are having problems with the battery, you will need to replace the battery or go to a repair shop.")
    elif "charging" in varSplit:
        time.sleep(0.75)
        print ("If you are having charging issues, you will have to replace the charger or you will have to get the charging port fixed.")
    elif "software" in varSplit:
        time.sleep(0.75)
        print ("If you have software issues, you will need to reset the phone or updating the software.")
    elif "volume" in varSplit:
        time.sleep(0.75)
        print ("If you have problems with the volume button, you will have go to a repair shop.")
    
    time.sleep(0.75)
    print("Thank you for your service ")


    answer = input("Would you like to start again?. ").lower().strip()
    if answer == "no":
            exit()

#2

What are you planning to do with pickle?


#3

This import can be done at run time, outside of any loop or function. Once the module is imported it remains present for the entire session so does not need to be imported again.

You could extend your if..elif statement with more cases. However, it might make more sense to use a look-up list, instead, Then it will be easy to extend without adding a lot of repetitive logic.

def lookup(keyword):
    keywords = {
        "display": "If the display isn't working, ...",
        "water": "...",
        "audio": "...",
        "battery": "...",
        "charging": "...",
        "software": "...",
        "volume": "..."
    }
    try:
        return keywords[keyword]
    except KeyError:
        return False

In the main code,

for kwd in varSplit:
    klu = lookup(kwd)
    if klu:
        print (klu)
        break
else:
    print ("Keyword not found in list.")

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.