HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

A phone troubleshooting program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
programphonetroubleshooting

Problem

The code should ask the user multiple solutions and find the problem with the users phone. The program should be efficient as possible and have a possible of 10 outcomes. I would like some tips on how I can make the code more efficient and meet the task requirements. This is what the task states:


Analyse the requirements for this system and design, develop, test and evaluate a program to
interrogate the user about the nature of the device and the problem, leading to a solution or advice to
call the supplier. You will need to create a suitable troubleshooting tree for a mobile device. This should
be a demonstration version of the system with at least 10 possible outcomes, but need not include an
exhaustive list of potential problems or outcomes.

```
import webbrowser
import random
print("Welcome to a phone troubleshooting program")
sol1 = ("Keep holding the power on button until the screen is on. If it doesn't turn on, contact your phone provider to get a replacement.")
sol2 = ("Charge your phone fully and switch it on.")
sol3 = ("Delete some apps, you need a minimum of at least 5 GB of free memory to run the phone correcly.You are probably out of memory.")
sol4 = ("Reset your phone.")
sol5 = ("You need a screen replacement. Either got to a shop or ask your insurance company covers the phone")
sol5 = ("You need to get your buttons replaced!")
sol6 = ("Get a screen replacement or contact your phone provider to get a phone replacement.")
sol7 = ("You dont need to do anything. Your phone doesnt have any problems.")
sol8 = ("Please update your phone software.")
sol9 = ("The best advice that I would suggest is to either contact apple for support or visit a shop to get it fixed")
sol10 = ("Take the phone and put it in a bag of rice for 24-36 hours to let the rice absorb the water.")
while True:
def apple():
prob_1 = None
while prob_1 not in ('software','hardware'):
prob_1 = input("Is your problem hardware or software").lowe

Solution

Data-structure based programming

In your current code you are building a tree of the possible answers with nested if, elif and while, reaching a nesting depth of 9, rendering it very hard to guess where the an additional troubleshooting recommendation should be added.

I suggest listing the solution to each case in a dictionary, and index it with the given user input, you can see an example down here:

SOLUTIONS = { {"android" : True, "software" : True, "turns_on" : False} : "Keep holding the power on button until the screen is on", 
              ... }

case = { "android"  : yes_no("Is it android?"),
         "software" : yes_no("Is it software"),
         "turns_on" : yes_no("Does it turn on?")     }

print(SOLUTIONS[case])


Handling cases for which you have no suggestion may be done with try, except, instead in your code you repeat the i don't know code many times:

try:
    print(SOLUTIONS[case])
except KeyError:
    print("I do not know the solution to this case, better ask official support")

Code Snippets

SOLUTIONS = { {"android" : True, "software" : True, "turns_on" : False} : "Keep holding the power on button until the screen is on", 
              ... }


case = { "android"  : yes_no("Is it android?"),
         "software" : yes_no("Is it software"),
         "turns_on" : yes_no("Does it turn on?")     }

print(SOLUTIONS[case])
try:
    print(SOLUTIONS[case])
except KeyError:
    print("I do not know the solution to this case, better ask official support")

Context

StackExchange Code Review Q#122068, answer score: 4

Revisions (0)

No revisions yet.