-2

I am fairly new to python, i have created this code for a controlled assessment but it is suggesting that 'q11' (the first of the web opening commands) is not defined. It is the same as the others and was working fine before but now i have begun work on it again but it just won't work.

Thank you in advance

Here is my code:

import webbrowser
import random
sol1 = ("Check if there is lint in the charging ports. This can be removed            carefully with a toothpick or similar implement")
sol2 = ("Turn on assistive touch if you have an iphone (settings > general >    accessability > assistive touch until you go to a shop to get them replaced. If  you use android, download 'button savior' from the google play store")
sol3 = ("Do a hard reset - hold down the power and home buttons until the screen  turns off and keep them held down until the screen turns on again ")
sol4 = ("Restore the phone to factory settings and set it up as new")
sol5 = ("You need a screen replacement.")
sol6 = ("You may need to replace the battery.")
sol7 = ("You dont need to do anything. Your phone doesnt have any problems.")
sol8 = ("Please update your phone software.")
sol9 = ("Contact apple for support")
sol10 = ("Take the phone and put it in a bag of rice for  24-36 hours to let the rice absorb the water.")


q1=input("Is your phone charging correctly? ")
 if q1 == "no":
    print(sol1)
if q1 == "yes":

q2=input("Is your phone water damaged? ")
  if q2 == "yes":
      print(sol10)
if q2 == "no":

q3=input("Is the screen cracked or badly scratched? ")
 if q3 == "yes":
     print(sol5)
if q3 == "no":

q4=input("Is the phone working slowly and crashing? ")
 if q4 == "yes":
     print(sol3)
if q4 == "no":

q5=input("Do you wish to remove data from the phone? ")
 if q5 == "yes":
     print(sol4)
if q5 == "no":

q6=input("Does the phone work without issues? ")
 if q6 == "yes":
     print(sol7)
if q6 == "no":

q7=input("Are you running the lastest software version? ")
  if q7 == "no":
     print(sol8)
if q7 == "yes":

q8=input("Are the buttons producing accurate responses ")
 if q8 == "no":
     print(sol2)
if q8 == "yes":

q9=input("Is your phone battery draining and dying early? ")
 if q9 == "yes":
    print(sol6)
if q9 == "no":

q10=input("Does the phone turn on, even if it has been charged with a working charger? ")
 if q10 == "yes":
     print(sol9)
if q10 == "no":

q11=input("Would you like to visit the apple support site?: yes/no ")
 if q11 == "yes":
      webbrowser.open("https://support.apple.com/en-gb")
if q11 == "no":

q12=input("Would you like to visit the genius bar booking site?: yes/no ")
 if q12 == "yes":
      webbrowser.open("https://getsupport.apple.com/")
 if q12 == "no":

print("Thank you for using this service. We hope you found it useful")

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
dbs
  • 1
  • 1
  • 2
  • 6
    Please fix your indentation. – Daniel Roseman Sep 30 '16 at 09:46
  • 1
    1) Expecting end users to input EXACTLY "yes" or "no" is risky. You might want to change your questions to `Is the phone working slowly and crashing? (Y/N)` to make sure the user is answering what you want him to answer. 2) These cascading `if` are terrible design. Change the `print` to `return` and remove the second test on the variable. – Efferalgan Sep 30 '16 at 10:13

3 Answers3

-1

Alright, been working on this for some time for you and I have added the following bits of code:

list = []
q1 = str(input("Is your phone charging correctly? "))
characters = len(q1)
for i in range(characters):
    lowercase = str.lower(q1[i])
    list.append(lowercase)
q1 = ("".join(list))

The characters variable counts the amount of characters entered in q1, eg if they typed 'yes' it would count 3 characters, 'no' it would count 2 characters etc.

This lowercase variable bascially sets whatever they input into lowercase looping through the whole string. We created the for loop looping the amount of characters. (Example: they input 'YES' then the length would be saved as 3 and it would loop through each character in the string 'YES' changing it all to 'yes'.)

The reason we need the length of the string is because without it we could not loop through the correct amount of times. (Example: if we just put a loop of 2 then if they inputted 'YES' it would only go through the string twice and would save as 'ye' and not include the third character. Same if the loop was 2 it would save it as 'y'.

list.append(lowercase)

This list.append basically adds the letter 'y' on the first loop into a list like this ['y'] then on the second loop it adds 'e' like this ['y','e'] and the third loop ['y','e','s']

q1 = ("".join(list))

This part basically combines the list we just made ['y','e','s'] into a string called q1 which is the variable for your answers. It combines it into 'yes'.

The reason I added the changing to lowercase in the first place is incase they enter 'Yes' or 'yES' or 'YES'... etc it will always be changed to all lowercase so that the answers can still work.

import webbrowser
import random
sol1 = ("Check if there is lint in the charging ports. This can be removed                carefully with a toothpick or similar implement")
sol2 = ("Turn on assistive touch if you have an iphone (settings > general >    accessability > assistive touch until you go to a shop to get them replaced. If  you use android, download 'button savior' from the google play store")
sol3 = ("Do a hard reset - hold down the power and home buttons until the screen  turns off and keep them held down until the screen turns on again ")
sol4 = ("Restore the phone to factory settings and set it up as new")
sol5 = ("You need a screen replacement.")
sol6 = ("You may need to replace the battery.")
sol7 = ("You dont need to do anything. Your phone doesnt have any problems.")
sol8 = ("Please update your phone software.")
sol9 = ("Contact apple for support")
sol10 = ("Take the phone and put it in a bag of rice for  24-36 hours to let the rice absorb the water.")

list = []
q1 = str(input("Is your phone charging correctly? "))
characters = len(q1)
for i in range(characters):
    lowercase = str.lower(q1[i])
    list.append(lowercase)
q1 = ("".join(list))
if q1 == "no":
    print(sol1)

list = []
if q1 == "yes":
    q2 = str(input("Is your phone water damaged? "))
for i in range(len(q2)):
    lowercase = str.lower(q2[i])
    list.append(lowercase)
q2 = ("".join(list))
if q2 == "yes":
    print(sol10)

list = []
if q2 == "no":
    q3 = str(input("Is the screen cracked or badly scratched? "))
    for i in range(len(q3)):
        lowercase = str.lower(q3[i])
        list.append(lowercase)
q3 = ("".join(list))
if q3 == "yes":
    print(sol5)

list = []
if q3 == "no":
    q4 = str(input("Is the phone working slowly and crashing? "))
    for i in range(len(q4)):
        lowercase = str.lower(q4[i])
        list.append(lowercase)
q4 = ("".join(list))
if q4 == "yes":
    print(sol3)

list = []
if q4 == "no":
    q5 = str(input("Do you wish to remove data from the phone? "))
    for i in range(len(q5)):
        lowercase = str.lower(q5[i])
        list.append(lowercase)
q5 = ("".join(list))
if q5 == "yes":
    print(sol4)

list = []
if q5 == "no":
    q6 = str(input("Does the phone work without issues? "))
    for i in range(len(q6)):
        lowercase = str.lower(q6[i])
        list.append(lowercase)
q6 = ("".join(list))
if q6 == "yes":
    print(sol7)

list = []
if q6 == "no":
    q7 = str(input("Are you running the lastest software version? "))
    for i in range(len(q7)):
        lowercase = str.lower(q7[i])
        list.append(lowercase)
q7 = ("".join(list))
if q7 == "no":
    print(sol8)

list = []
if q7 == "yes":
    q8 = str(input("Are the buttons producing accurate responses "))
    for i in range(len(q8)):
        lowercase = str.lower(q8[i])
        list.append(lowercase)
q8 = ("".join(list))
if q8 == "no":
     print(sol2)

list = []
if q8 == "yes":
    q9 = str(input("Is your phone battery draining and dying early? "))
    for i in range(len(q9)):
        lowercase = str.lower(q9[i])
        list.append(lowercase)
q9 = ("".join(list))
if q9 == "yes":
    print(sol6)

list = []
if q9 == "no":
    q10 = str(input("Does the phone turn on, even if it has been charged with a working charger? "))
    for i in range(len(q10)):
        lowercase = str.lower(q10[i])
        list.append(lowercase)
q10 = ("".join(list))
if q10 == "yes":
     print(sol9)

list = []
if q10 == "no":
    q11 = str(input("Would you like to visit the apple support site?: yes/no "))
    for i in range(len(q11)):
        lowercase = str.lower(q11[i])
        list.append(lowercase)
q11 = ("".join(list))
if q11 == "yes":
    webbrowser.open("https://support.apple.com/en-gb")

list = []
if q11 == "no":
    q12 = str(input("Would you like to visit the genius bar booking site?: yes/no "))
    for i in range(len(q12)):
        lowercase = str.lower(q12[i])
        list.append(lowercase)
q12 = ("".join(list))

if q12 == "yes":
    webbrowser.open("https://getsupport.apple.com/")
else:
    print()
Sam.Natale
  • 50
  • 1
  • 1
  • 9
  • Please ask me any questions you may have. – Sam.Natale Sep 30 '16 at 11:09
  • I'm currently using your code that i marked as the best answer. However, the web links won't open and the code just ends after the 10th question. This was happening before and the syntax is identical to the other questions. Do you have any idea as to why this is happening? – dbs Oct 03 '16 at 09:24
-1

Alternatively you could just make it so they input either y or n which would be a lot less lines of code.

All I did differently from my previous answer is simply only made it loop once to change the 'Y' to lowercase or 'N' to lowercase. It's better to make it like this because when people use a program and are asked to input 'Y' they sometimes do 'y' instead, so always best to be sure.

Also adding elif instead of if for a second if statement on the same thing is neater programming, somehow python allows it but if you ever move on to another programing language you have to use elif so may aswell get used to it now.

sol1 = ("Check if there is lint in the charging ports. This can be removed            carefully with a toothpick or similar implement")

Another tip this variable above could be made into something a lot better than a spam of spaces, did you know '\n' in a string starts a new line.

import webbrowser
import random
sol1 = ("Check if there is lint in the charging ports. This can be removed\ncarefully with a toothpick or similar implement")
sol2 = ("Turn on assistive touch if you have an iphone (settings > general >\naccessability > assistive touch until you go to a shop to get them replaced If  you use android, download 'button savior' from the google play store")
sol3 = ("Do a hard reset - hold down the power and home buttons until the screen\nturns off and keep them held down until the screen turns on again ")
sol4 = ("Restore the phone to factory settings and set it up as new")
sol5 = ("You need a screen replacement.")
sol6 = ("You may need to replace the battery.")
sol7 = ("You dont need to do anything. Your phone doesnt have any problems.")
sol8 = ("Please update your phone software.")
sol9 = ("Contact apple for support")
sol10 = ("Take the phone and put it in a bag of rice for\n24-36 hours to let the rice absorb the water.")

q1 = str(input("Is your phone charging correctly? Y/N >> "))
for i in range(1):
    q1 = str.lower(q1)
if q1 == "n":
    print(sol1)

elif q1 == "y":
    q2 = str(input("Is your phone water damaged? Y/N >> "))
    for i in range(1):
        q2 = str.lower(q2)
if q2 == "y":
    print(sol10)

elif q2 == "n":
    q3 = str(input("Is the screen cracked or badly scratched? Y/N >> "))
    for i in range(1):
        q3 = str.lower(q3)
if q3 == "y":
    print(sol5)

elif q3 == "n":
    q4 = str(input("Is the phone working slowly and crashing? Y/N >> "))
    for i in range(1):
        q4 = str.lower(q4)
if q4 == "y":
    print(sol3)

elif q4 == "n":
    q5 = str(input("Do you wish to remove data from the phone? Y/N >> "))
    for i in range(1):
         q5 = str.lower(q5)
if q5 == "y":
    print(sol4)

elif q5 == "n":
    q6 = str(input("Does the phone work without issues? Y/N >> "))
    for i in range(1):
        q6 = str.lower(q6)
if q6 == "y":
    print(sol7)

elif q6 == "n":
    q7 = str(input("Are you running the lastest software version? Y/N >> "))
    for i in range(1):
        q7 = str.lower(q7)
if q7 == "n":
    print(sol8)

elif q7 == "y":
    q8 = str(input("Are the buttons producing accurate responses Y/N >> "))
    for i in range(1):
        q8 = str.lower(q8)
if q8 == "n":
     print(sol2)

elif q8 == "y":
    q9 = str(input("Is your phone battery draining and dying early? Y/N >> "))
    for i in range(1):
        q9 = str.lower(q9)
if q9 == "y":
    print(sol6)

elif q9 == "n":
    q10 = str(input("Does the phone turn on, even if it has been charged with a working charger? Y/N >> "))
    for i in range(1):
        q10 = str.lower(q10)
if q10 == "y":
     print(sol9)

elif q10 == "n":
    q11 = str(input("Would you like to visit the apple support site?: yes/no Y/N >> "))
    for i in range(1):
        q11 = str.lower(q11)
if q11 == "y":
    webbrowser.open("https://support.apple.com/en-gb")

elif q11 == "n":
    q12 = str(input("Would you like to visit the genius bar booking site?:  Y/N >> "))
    for i in range(1):
        q12 = str.lower(q12)

if q12 == "y":
    webbrowser.open("https://getsupport.apple.com/")
else:
    print()
Sam.Natale
  • 50
  • 1
  • 1
  • 9
-1

You could even use a dictionary to get rid of all those variables such as sol1, sol2, sol3. That is really messy coding and inefficient. Try this dictionary:

solutions = {1: "Check if there is lint in the charging ports. This can be removed\ncarefully with a toothpick or similar implement",
         2: "Turn on assistive touch if you have an iphone (settings > general >\naccessability > assistive touch until you go to a shop to get them replaced If  you use android, download 'button savior' from the google play store.",
         3: "Do a hard reset - hold down the power and home buttons until the screen\nturns off and keep them held down until the screen turns on again ",
         4: "Restore the phone to factory settings and set it up as new",
         5: "You need a screen replacement.",
         6: "You may need to replace the battery.",
         7: "You dont need to do anything. Your phone doesnt have any problems.",
         8: "Please update your phone software.",
         9: "Contact apple for support",
         10: "Take the phone and put it in a bag of rice for\n24-36 hours to let the rice absorb the water."
         }

It basically assigns the number 1: 'to the string here' and 2: 'to the string here'. It is pretty useful for what you are trying to do because making loads and loads of variables is not a clever thing to do as they can get confusing etc...

But to get it to print the correct solution all you have to do is print(solutions[1]) for solution 1 or print(solutions[2]) for solution 2. Hope this helps!

Full code:

solutions = {1: "Check if there is lint in the charging ports. This can be removed\ncarefully with a toothpick or similar implement",
         2: "Turn on assistive touch if you have an iphone (settings > general >\naccessability > assistive touch until you go to a shop to get them replaced If  you use android, download 'button savior' from the google play store.",
         3: "Do a hard reset - hold down the power and home buttons until the screen\nturns off and keep them held down until the screen turns on again ",
         4: "Restore the phone to factory settings and set it up as new",
         5: "You need a screen replacement.",
         6: "You may need to replace the battery.",
         7: "You dont need to do anything. Your phone doesnt have any problems.",
         8: "Please update your phone software.",
         9: "Contact apple for support",
         10: "Take the phone and put it in a bag of rice for\n24-36 hours to let the rice absorb the water."
         }
q1 = str(input("Is your phone charging correctly? Y/N >> "))
for i in range(1):
    q1 = str.lower(q1)
if q1 == "n":
    print(solutions[1])

elif q1 == "y":
    q2 = str(input("Is your phone water damaged? Y/N >> "))
    for i in range(1):
        q2 = str.lower(q2)
    if q2 == "y":
        print(solutions[2])

elif q2 == "n":
    q3 = str(input("Is the screen cracked or badly scratched? Y/N >> "))
    for i in range(1):
        q3 = str.lower(q3)
    if q3 == "y":
        print(solutions[3])

elif q3 == "n":
    q4 = str(input("Is the phone working slowly and crashing? Y/N >> "))
    for i in range(1):
        q4 = str.lower(q4)
    if q4 == "y":
        print(solutions[4])

elif q4 == "n":
    q5 = str(input("Do you wish to remove data from the phone? Y/N >> "))
    for i in range(1):
        q5 = str.lower(q5)
    if q5 == "y":
        print(solutions[4])

elif q5 == "n":
    q6 = str(input("Does the phone work without issues? Y/N >> "))
    for i in range(1):
        q6 = str.lower(q6)
    if q6 == "y":
        print(solutions[7])

elif q6 == "n":
    q7 = str(input("Are you running the lastest software version? Y/N >> "))
    for i in range(1):
        q7 = str.lower(q7)
    if q7 == "n":
        print(solutions[8])

elif q7 == "y":
    q8 = str(input("Are the buttons producing accurate responses Y/N >> "))
    for i in range(1):
        q8 = str.lower(q8)
    if q8 == "n":
         print(solutions[2])

elif q8 == "y":
    q9 = str(input("Is your phone battery draining and dying early? Y/N >> "))
    for i in range(1):
        q9 = str.lower(q9)
    if q9 == "y":
        print(solutions[6])

elif q9 == "n":
    q10 = str(input("Does the phone turn on, even if it has been charged with a working charger? Y/N >> "))
    for i in range(1):
        q10 = str.lower(q10)
    if q10 == "y":
         print(solutions[9])

elif q10 == "n":
    q11 = str(input("Would you like to visit the apple support site?: yes/no Y/N >> "))
    for i in range(1):
        q11 = str.lower(q11)
    if q11 == "y":
        webbrowser.open("https://support.apple.com/en-gb")

elif q11 == "n":
    q12 = str(input("Would you like to visit the genius bar booking site?:  Y/N >> "))
    for i in range(1):
        q12 = str.lower(q12)

    if q12 == "y":
        webbrowser.open("https://getsupport.apple.com/")
    else:
        print()
Sam.Natale
  • 50
  • 1
  • 1
  • 9