0
try塊根本沒有運行...有人可以找出原因嗎?嘗試/除非程序塊未運行
case = open("phone.txt", "r+")
phone = ["What brand of phone do you have?", "What model?", "What version?", "How much memory?", "What seems to be the problem?"]
laptop = ["What brand of laptop do you have?", "What model?", "How much RAM?", "How much storage?", "What seems to be the problem?"]
tablet = ["What brand of tablet do you have?", "What model?", "What version?", "How much memory?", "What seems to be the probem?"]
specs = []
keyword = ["crack", "cracked", "wet", "water", "sound", "charge", "charger", "smashed", "slow", "boot", "startup", "blue", "responding", "", "", ""]
possibeSolutions = ["System: 'You should take your phone to get the screen replaced.'", "System: 'You should take your phone to get the screen replaced.'", "System: 'You should dry your device in rice.'", "", "", "", "", "", "", "", "", "", "", "", "", ""]
count = 0
device = input("System: 'What type of device do you have?'\nYou: ")
specs.append(device)
def problems(device, brand, model, version, memory, problem):
solution = False
specs = [brand, model, version, memory, problem]
problem2 = problem.split(" ")
for i in problem2:
for n in keyword:
if i.lower() == n.lower():
print(possibeSolutions[keyword.index(n)])
solution = True
if solution == False:
try:
case2 = case.readlines()
fileLen = len(case2)
caseNum = case2[fileLen - 2]
caseNum = int(caseNum) + 1
print("...")
except ValueError:
caseNum = 1
print("...")
finally:
print("...")
case.write(str(caseNum) + "\n(" + str(specs) + ")\n")
print("System: 'Your problem has been added the the database and one of our engineers will check it out later.'")
if "phone" in device.lower():
for i in phone:
print("System: '" + i + "'")
answer = input("You: ")
specs.append(answer)
elif "laptop" in device.lower():
for i in laptop:
print("System: '" + i + "'")
answer = input("You: ")
specs.append(answer)
elif "tablet" in device.lower():
for i in tablet:
print("System: '" + i + "'")
answer = input("You: ")
specs.append(answer)
else:
print("System: 'Sorry, this troubleshooting program does not support that device.'")
try:
problems(specs[0], specs[1], specs[2], specs[3], specs[4], specs[5])
except IndexError:
pass
print("System: 'The program has ended.'")
case.close()
我曾嘗試在try塊中加入一些印花和不打印出來。只有finally塊正在運行,這顯然會拋出一個錯誤,因爲caseNum沒有被定義爲它在try/except塊中定義的。 (這是def的嘗試,這裏是問題)。
請把它降低到[mcve]。你可以在這個過程中解決你自己的問題。 – jonrsharpe
你能給我們一個樣本輸入來運行這個方法嗎? – gowrath
我只收到finally塊的錯誤信息,因爲try塊沒有運行,我可以看到「...」沒有爲try塊打印輸出。 –