Search code examples
pythonundefineddefined

Python NameError: name '' is not defined


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")


Solution

  • 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()