Python NameError: name '' is not defined
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":
打印("Thank you for using this service. We hope you found it useful")
好的,已经为您处理了一段时间,我添加了以下代码:
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 中输入的字符数量,例如,如果他们输入 'yes',它将计算 3 个字符,'no',它将计算 2 个字符等。
这个小写变量基本上将他们输入的任何内容设置为遍历整个字符串的小写循环。我们创建了循环字符数量的 for 循环。 (例如:他们输入 'YES' 然后长度将被保存为 3 并且它将循环遍历字符串 'YES' 中的每个字符,将其全部更改为 'yes'。)
我们需要字符串长度的原因是没有它我们无法循环正确的次数。 (示例:如果我们只放一个 2 的循环,那么如果他们输入 'YES',它只会遍历字符串两次并保存为 'ye',并且不包括第三个字符。如果循环是 2,则相同它会将其保存为 'y'.
list.append(lowercase)
这个 list.append 基本上在第一个循环中将字母 'y' 添加到这样的列表中 ['y'] 然后在第二个循环中它添加 'e' 像这样['y','e'] 和第三个循环 ['y','e','s']
q1 = ("".join(list))
这部分基本上将我们刚刚制作的列表 ['y','e','s'] 组合成一个名为 q1 的字符串,它是您答案的变量。它组合成 'yes'.
我首先添加更改为小写的原因是如果他们输入 'Yes' 或 'yES' 或 '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:'to the string here' 和 2:'to the string here'。这对您尝试做的事情非常有用,因为加载和加载变量并不是一件聪明的事情,因为它们会让人感到困惑等...
但要让它打印出正确的解决方案,您所要做的就是针对解决方案 1 打印(solutions[1]) 或针对解决方案 2 打印(solutions[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()
我是 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":
打印("Thank you for using this service. We hope you found it useful")
好的,已经为您处理了一段时间,我添加了以下代码:
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 中输入的字符数量,例如,如果他们输入 'yes',它将计算 3 个字符,'no',它将计算 2 个字符等。
这个小写变量基本上将他们输入的任何内容设置为遍历整个字符串的小写循环。我们创建了循环字符数量的 for 循环。 (例如:他们输入 'YES' 然后长度将被保存为 3 并且它将循环遍历字符串 'YES' 中的每个字符,将其全部更改为 'yes'。)
我们需要字符串长度的原因是没有它我们无法循环正确的次数。 (示例:如果我们只放一个 2 的循环,那么如果他们输入 'YES',它只会遍历字符串两次并保存为 'ye',并且不包括第三个字符。如果循环是 2,则相同它会将其保存为 'y'.
list.append(lowercase)
这个 list.append 基本上在第一个循环中将字母 'y' 添加到这样的列表中 ['y'] 然后在第二个循环中它添加 'e' 像这样['y','e'] 和第三个循环 ['y','e','s']
q1 = ("".join(list))
这部分基本上将我们刚刚制作的列表 ['y','e','s'] 组合成一个名为 q1 的字符串,它是您答案的变量。它组合成 'yes'.
我首先添加更改为小写的原因是如果他们输入 'Yes' 或 'yES' 或 '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:'to the string here' 和 2:'to the string here'。这对您尝试做的事情非常有用,因为加载和加载变量并不是一件聪明的事情,因为它们会让人感到困惑等...
但要让它打印出正确的解决方案,您所要做的就是针对解决方案 1 打印(solutions[1]) 或针对解决方案 2 打印(solutions[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()