繁体   English   中英

Python NameError: 名称 '' 未定义

[英]Python NameError: name '' is not defined

我对 python 相当陌生,我为受控评估创建了此代码,但它表明未定义“q11”(第一个 Web 打开命令)。 它和其他的一样,以前工作得很好,但现在我又开始工作了,但它不起作用。

先感谢您

这是我的代码:

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("感谢您使用此服务。我们希望您觉得它有用")

好的,已经为您研究了一段时间,我添加了以下代码:

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

characters 变量计算 q1 中输入的字符数,例如,如果他们输入“是”,则将计算 3 个字符,“否”将计算 2 个字符等。

这个小写变量基本上将它们输入的任何内容设置为小写循环遍历整个字符串。 我们创建了循环字符数量的 for 循环。 (例如:他们输入 'YES' 然后长度将被保存为 3,它会遍历字符串 'YES' 中的每个字符,将其全部更改为 'yes'。)

我们需要字符串长度的原因是因为没有它我们无法循环正确的次数。 (例如:如果我们只放一个 2 的循环,那么如果他们输入 'YES',它只会遍历字符串两次并保存为 'ye' 并且不包括第三个字符。如果循环是 2,它会保存它作为'y'。

list.append(lowercase)

这个 list.append 基本上将第一个循环中的字母 'y' 添加到这样的列表 ['y'] 然后在第二个循环中它添加这样的 'e' ['y','e'] 和第三个循环['是的']

q1 = ("".join(list))

这部分基本上将我们刚刚制作的列表 ['y','e','s'] 组合成一个名为 q1 的字符串,它是您答案的变量。 它将它组合成“是”。

我首先将更改添加为小写的原因是,如果他们输入“是”或“yES”或“是”...等,它将始终更改为全部小写,以便答案仍然有效。

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

或者,您可以这样做,以便他们输入 y 或 n,这将减少很多代码行。

我所做的与我之前的答案不同的只是让它循环一次以将“Y”更改为小写或将“N”更改为小写。 最好这样做,因为当人们使用程序并被要求输入“Y”时,他们有时会输入“y”,因此最好始终确保。

在同一件事上添加 elif 而不是 if 的第二个 if 语句是更简洁的编程,python 以某种方式允许它,但如果你转向另一种编程语言,你必须使用 elif 所以现在可能已经习惯了。

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

另一个提示,上面的这个变量可以变成比垃圾邮件更好的东西,你知道字符串中的 '\\n' 会开始一个新行吗?

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

您甚至可以使用字典来删除所有这些变量,例如 sol1、sol2、sol3。 那真的是很乱的编码而且效率低下。 试试这本词典:

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."
         }

它基本上分配数字 1:'这里的字符串'和 2:'这里的字符串'。 它对您尝试做的事情非常有用,因为加载和加载变量并不是一件聪明的事情,因为它们可能会让人感到困惑等等......

但是要让它打印正确的解决方案,您所要做的就是为解决方案 1 打印(解决方案 [1])或为解决方案 2 打印(解决方案 [2])。希望这会有所帮助!

完整代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM