How Do You Add A List To Jump To A Line On My Code?
I Have a python code to fix phones and i was wondering what was the best way to jump to a particular question when i type in for example 'Broken Screen' im really stuck and need t
Solution 1:
Here's the best attempt I had at replicating your code. I made a couple of the problems def() so that you can call each one separately. I hope this is what you wanted!
defmenu():
print("Welcome to Sams Phone Troubleshooting program")
print("Please Enter your name")
name=input()
print("Thanks for using Kierans Phone Troubleshooting program "+name +"\n")
defstart():
select = " "print("Would you like to start this program? Please enter either y for yes or n for no")
select=input()
if select=="y":
troubleshooter()
elif select=="n":
quit
else:
print("Invalid please enter again")
deftroubleshooter():
print("""Please choose the problem you are having with your phone (input 1-4):
1) My phone doesn't turn on
2) My phone is freezing
3) The screen is cracked
4) I dropped my phone in water\n""")
problemselect = int(input())
if problemselect ==1:
not_on()
elif problemselect ==2:
freezing()
elif problemselect ==3:
cracked()
elif problemselect ==4:
water()
start()
defnot_on():
print("Have you plugged in the charger?")
answer = input()
if answer =="y":
print("Charge it with a diffrent charger in a diffrent phone socket. Does it work?")
else:
print("Plug it in and leave it for 20 mins, has it come on?")
answer = input()
if answer=="y":
print("Are there any more problems?")
else:
print("Restart the troubleshooter or take phone to a specialist\n")
answer=input()
if answer =="y":
print("Restart this program")
else:
print("Thank you for using my troubleshooting program!\n")
deffreezing():
print("Charge it with a diffrent charger in a diffrent phone socket")
answer = input("Are there any more problems?")
if answer=="y":
print("Restart the troubleshooter or take phone to a specialist\n")
else:
print("Restart this program\n")
defcracked():
answer =input("Is your device responsive to touch?")
if answer=="y":
answer2 = input("Are there any more problems?")
else:
print("Take your phone to get the screen replaced")
if answer2=="y":
print("Restart the program or take phone to a specialist\n")
else:
print("Thank you for using my troubleshooting program!\n")
defwater():
print("Do not charge it and take it to the nearest specialist\n")
menu()
whileTrue:
start()
troubleshooter()
Hope this helps somewhat and if there are minor problems with the code then just message me! (I'm relatively new to this site!)
Solution 2:
The following code should provide a framework on which to build and extend the program in your question. Most of the code should be fine as it currently written, but you can expand its functionality if needed. To continue building on what questions can be asked and what answers are given, consider adding more sections to the database at the top of the file. The cases are can be easily augmented.
#! /usr/bin/env python3"""Cell Phone Self-Diagnoses Program
The following program is designed to help users to fix problems that they may
encounter while trying to use their cells phones. It asks questions and tries
to narrow down what the possible cause of the problem might be. After finding
the cause of the problem, a recommended action is provided as an attempt that
could possibly fix the user's device."""# This is a database of questions used to diagnose cell phones.
ROOT = 0
DATABASE = {
# The format of the database may take either of two forms:# LABEL: (QUESTION, GOTO IF YES, GOTO IF NO)# LABEL: ANSWER
ROOT: ('Does your phone turn on? ', 1, 2),
1: ('Does it freeze? ', 11, 12),
2: ('Have you plugged in a charger? ', 21, 22),
11: ('Did you drop your device in water? ', 111, 112),
111: 'Do not charge it and take it to the nearest specialist.',
112: 'Try a hard reset when the battery has charge.',
12: 'I cannot help you with your phone.',
21: 'Charge it with a different charger in a different phone ''socket.',
22: ('Plug it in and leave it for 20 minutes. Has it come on? ',
221, 222),
221: ('Are there any more problems? ', 222, 2212),
222: ('Is the screen cracked? ', 2221, 2222),
2212: 'Thank you for using my troubleshooting program!',
2221: 'Replace in a shop.',
2222: 'Take it to a specialist.'
}
# These are possible answers accepted for yes/no style questions.
POSITIVE = tuple(map(str.casefold, ('yes', 'true', '1')))
NEGATIVE = tuple(map(str.casefold, ('no', 'false', '0')))
defmain():
"""Help diagnose the problems with the user's cell phone."""
verify(DATABASE, ROOT)
welcome()
ask_questions(DATABASE, ROOT)
defverify(database, root):
"""Check that the database has been formatted correctly."""
db, nodes, visited = database.copy(), [root], set()
while nodes:
key = nodes.pop()
if key in db:
node = db.pop(key)
visited.add(key)
ifisinstance(node, tuple):
iflen(node) != 3:
raise ValueError('tuple nodes must have three values')
query, positive, negative = node
ifnotisinstance(query, str):
raise TypeError('queries must be of type str')
iflen(query) < 3:
raise ValueError('queries must have 3 or more characters')
ifnot query[0].isupper():
raise ValueError('queries must start with capital letters')
if query[-2:] != '? ':
raise ValueError('queries must end with the "? " suffix')
ifnotisinstance(positive, int):
raise TypeError('positive node names must be of type int')
ifnotisinstance(negative, int):
raise TypeError('negative node names must be of type int')
nodes.extend((positive, negative))
elifisinstance(node, str):
iflen(node) < 2:
raise ValueError('string nodes must have 2 or more values')
ifnot node[0].isupper():
raise ValueError('string nodes must begin with capital')
if node[-1] notin {'.', '!'}:
raise ValueError('string nodes must end with "." or "!"')
else:
raise TypeError('nodes must either be of type tuple or str')
elif key notin visited:
raise ValueError('node {!r} does not exist'.format(key))
if db:
raise ValueError('the following nodes are not reachable: ' +
', '.join(map(repr, db)))
defwelcome():
"""Greet the user of the application using the provided name."""print("Welcome to Sam's Phone Troubleshooting program!")
name = input('What is your name? ')
print('Thank you for using this program, {!s}.'.format(name))
defask_questions(database, node):
"""Work through the database by asking questions and processing answers."""whileTrue:
item = database[node]
ifisinstance(item, str):
print(item)
breakelse:
query, positive, negative = item
node = positive if get_response(query) else negative
defget_response(query):
"""Ask the user yes/no style questions and return the results."""whileTrue:
answer = input(query).casefold()
if answer:
ifany(option.startswith(answer) for option in POSITIVE):
returnTrueifany(option.startswith(answer) for option in NEGATIVE):
returnFalseprint('Please provide a positive or negative answer.')
if __name__ == '__main__':
main()
Post a Comment for "How Do You Add A List To Jump To A Line On My Code?"