0

I have currently made a python program that is supposed to take a users input, find the keyword and find a text to go with it. One problem: It doesn't work. Code will be below:

import time
import sys
e=0
brokenscreen= "crack cracked smashed".split()
newtest= "turning turn".split()
buttons= "buttons button".split()
sound= "sound speaker volume".split()
frozen= "frozen unresponsive responding".split()
WiFi= "wi-fi wifi disconnecting wifi".split()
memory= "memory data".split()
removeapps= "uninstall removeapps appremove uninstal apps app".split()
wipe= "completerestart restart wipe".split()
water= "water wet".split()
camera= "camera cam".split()
bad= "bad rubbish terrible".split()
black= "black calls".split()
notifications= "notifications"
anlist=[brokenscreen, newtest, buttons, sound, frozen, WiFi, memory,         removeapps, wipe, water, camera, bad, black, notifications]
anPrint=[
"You need to repair it in a store.",
"Check that it has full charge, or check it is definately charging. You will         need to take it to the store to get it checked.",
"Button Savior will bring your phone hardware keys to your home screen.     After you install and run the app, you will see a small pull out arrow on the right edge of the screen.\n Tap on it and yu should have buttons on screen.",
"If your phone suffer from low volume then you can download AudioBoost from the market. It can increase the volume up to 30% and is also activated from home screen widget.",
"You just need to restart your phone to kickstart a frozen phone.",
"This may solve the problem. Just go to Wi-Fi > Settings > Menu > Advanced and choose to stay connected to Wi-Fi during sleep.",
"Among other reasons, the most prominent problem can be a full cache which affects the efficient running of apps. You can download apps like Cache Cleaner or Clean Master from Google Play",
"Go to Settings > Applications > Manage Applications and then select the app you wish to uninstall. Now tap the Uninstall icon to remove the app.",
"Go to Settings > SD & Phone storage > Factory Data Restore. Go through the process twice to be completely sure that everything has been removed.",
"Take the battery out as quickly as possible and dry as many parts of the phone as you can then leave it for 72 hours in a dry room. \n Try it again, it may work. Also try putting the phone and components into a bowl of rice.",
"If the camera doesn't work, let the camera take the photo at the default resolution, then scale it to your preferred size in an AsyncTask, an app on the app store.",
"I'd reccomend android. Anything but apple. Get a new phone.",
"Some people have reported success by pushing on the screen where the proximity sensor is located, which suggests a build issue. There is no fix for this issue yet.",
"Check your Wi-Fi or data connection. Poor coverage will impact on push notifications. Go to Settings > Wi-Fi > Advanced and tick the box next to Keep WiFi on when screen times out."
]

def restart():
    yes_no=input("Restart? (Y/n): ").lower()
    if yes_no:
       return yes_no[0] == 'y'
    return True

def program(pos):
    if z=="an" or z=="and" or z=="android":
          for word in a:
              item=anlist[pos]
              if word in item:
                  print(anPrint[pos])
                  time.sleep(0.5)
                  pos=pos+1
              elif word not in anlist:
                  pos=pos+1
                  sys.exit
    return pos

while True:
    z=input("ANDROID™ TROUBLESHOOTING (Alpha Testing 0.9.5) Enter the word 'Android': ")
    if z=="and" or z=="an" or z=="android":
        a=input("Describe your problem please: ").lower().split()
        program(e)
        restart()
    else:
        print("No valid input detected.")
        restart()
if not restart():
    break

It works perfectly well when I use something like 1 keyword, it finds the sentence to go with it and executes almost perfectly, but I have problems when I try to restart (Labeled with "repeat question") and actually printing out the statement when the user writes a sentence. Any help is appreciated thanks guys

EDIT: Fixed restart

7
  • 2
    Are you sure this is your exact up-to-date code? Because it doesn't say "repeat question" anywhere, and there's an infinite while loop around your first function definition, and an IndentationError around your first elif. It doesn't even run on my machine. Commented Feb 23, 2017 at 20:37
  • for word in anlist: for word in a: the word in a overrides the word in anlist, so any reference to word references a... which as far as I can tell is undefined at that point. Commented Feb 23, 2017 at 20:38
  • a is the input which the user gives, I meant "repeat question" and it uses Python 3. It should run on your machine, but I am a bit of a noob at python, any ideas on how to make this... good? Commented Feb 23, 2017 at 20:46
  • 1
    Um... do you not have problems with the while True: def restart(): ... block? Doesn't that just define the restart function an infinite number of times? O.o Commented Feb 23, 2017 at 20:50
  • 1
    Edit your question-- don't put it as an answer. That's not the right context for it on StackOverflow. Commented Feb 23, 2017 at 21:41

1 Answer 1

1

You can use a dictionnary like this :

word = 'test'
topics = {
'brokenscreen': "crack cracked smashed".split(),
'newtest': "turning turn test".split(),
'buttons': "buttons button test".split()
}
possibletopics = [topic for topic, words in topics.items() if word in words]
print(possibletopics)

Be careful there may be multiple hits as in the suggested code.

Then you may use an advice dict like this :

{"brokenscreen": "You need to repair it in a store.",
 "newtest": "Check that it has full charge, or ch ..."}

A better way (in this case) would be to store in such a dict :

{"brokenscreen":{'keywords':  "crack cracked smashed".split(),
                 'advice': "You need to repair it in a store."}
 "foo": {"keywords": ['bla', 'blo'],
         "advice": 'bla bla bla'}
 }

List comprehension and data model

You can end up with the folowing cleaned code :

help_base = {"brokenscreen":{'keywords':  "crack cracked smashed".split(),
                 'advice': "You need to repair it in a store."},
            "foo": {"keywords": ['bla', 'blo'],
                    "advice": 'bla bla bla'}
            }

def restart():

    yes_no=input("Restart? (Y/n): ").lower()
    if yes_no:
        return yes_no[0] == 'y'
    return True


input("ANDROID™ TROUBLESHOOTING (Alpha Testing 0.9.5) press Enter")
while True:
    submitted_words = input("Describe your problem please: ").lower().split()
    for word in submitted_words:
        for label, text in help_base.items():
            if word in text['keywords']:
                print(text['advice'])

    if not restart():
        break
Sign up to request clarification or add additional context in comments.

1 Comment

Your're wielcome. Please remind to accept the answer by the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.