Python名称错误:名称'';没有定义
我对python相当陌生,我创建了这段代码用于受控评估,但它表明“q11”(第一个web打开命令)没有定义。它和其他的一样,以前工作得很好,但现在我又开始工作了,但它就是不工作 先谢谢你 这是我的密码:Python名称错误:名称'';没有定义,python,undefined,defined,Python,Undefined,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
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的循环,那么如果他们输入“是”,它将只遍历字符串两次,并将另存为“ye”,而不包括第三个字符。如果循环为2,则将其另存为“y”
list.append(lowercase)
This list.append基本上是将第一个循环中的字母“y”添加到类似于['y']的列表中,然后在第二个循环中添加类似于['y','e']的字母“e”,以及第三个循环['y','e','s']
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()
请修复缩进。1)期望最终用户准确输入“是”或“否”是有风险的。您可能想将问题改为
手机是否工作缓慢且崩溃?(是/否)
以确保用户回答了您希望他回答的问题。2) 这些级联的if
是糟糕的设计。将打印
更改为返回
,并删除对变量的第二个测试。请询问我您可能有的任何问题。我目前正在使用您的代码,我将其标记为最佳答案。然而,网页链接不会打开,代码在第10个问题之后就结束了。这以前发生过,语法与其他问题相同。你知道为什么会发生这种情况吗?我在这两个代码版本上花了很多时间,选择一个你最了解/与你的课程工作最相关的。祝你好运,有问题问我:太感谢你了!这真的很有帮助,5)/“谢谢你的时间:)设一个为最佳答案?好了,我添加了3个答案。这个答案是最好的,字典更实用,也不那么凌乱。