让代码在一个变量下问一组不同的问题

2024-04-26 15:12:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个程序,这是一个电话故障排除识别关键字,然后读取和打印出一个文件,其中有解决方案,它取决于所说的关键字。现在用我的代码,我问问题是什么,如果我说liquid,我想打印liquid文档,并提出一系列与liquid问题相关的问题。当我这么做的时候,它总是问第一组问题,那就是力量。有谁能帮我把问题转到正确的方向吗

#Task 2 Trouble-Shooter 1.0
#Zacharriah River Howells
import time
print('Hello and welcome to the mobile phone Trouble-Shooter!')
time.sleep(2)
problem = input('Please input what is wrong with your phone')
if "power" in problem:
    f = open('Power.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
if "battery" in problem:
    f = open('Power.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
time.sleep(2)

print('Have you charged your phone overnight? if not do so')
time.sleep(1)
powerans = input('Does your phone turn on now?')
if powerans == 'yes':
    f = open('Power.txt', 'r')
    solution1 = f.readlines()
    print(solution1[1])
    f.close()
    exit()

elif powerans == 'no':
    f = open('Power.txt', 'r')
    solution2 = f.readlines()
    print(solution2[2])
    f.close()
    exit()

if "liquid" in problem:
    f = open('Liquid.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
if "water" in problem:
    f = open('Liquid.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
time.sleep(2)

print('Have you let your phone dry in a container filled with rice?')
time.sleep(1)
liquidans = input('Does your phone turn on now?')
if liquidans == 'yes':
    f = open('Liquid.txt', 'r')
    solution1 = f.readlines()
    print(solution3[1])
    f.close()
    exit()

elif liquidans == 'no':
    f = open('Liquid.txt', 'r')
    solution2 = f.readlines()
    print(solution4[2])
    f.close()
    exit()

if "software" in problem:
    f = open('Software.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
if "apps" in problem:
    f = open('Software.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
time.sleep(2)

print('Have you tried reinstalling the app?')
time.sleep(1)
softeareans = input('Does your phone turn on now?')
if softwareans == 'yes':
    f = open('Software.txt', 'r')
    solution1 = f.readlines()
    print(solution5[1])
    f.close()
    exit()

elif softwareans == 'no':
    f = open('Software.txt', 'r')
    solution2 = f.readlines()
    print(solution6[2])
    f.close()
    exit()

if "hardware" in problem:
    f = open('Hardware.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
if "display" in problem:
    f = open('Hardware.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
time.sleep(2)

print('Have you tried installing the newest system update?')
time.sleep(1)
hardwareans = input('Does your phone turn on now?')
if hardewareans == 'yes':
    f = open('Hardware.txt', 'r')
    solution1 = f.readlines()
    print(solution7[1])
    f.close()
    exit()

elif hardwareans == 'no':
    f = open('Hardware.txt', 'r')
    solution2 = f.readlines()
    print(solution8[2])
    f.close()
    exit()

if "unable" in problem:
    f = open('Unidentifiable.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
if "dunno" in problem:
    f = open('Unidentifiable.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
time.sleep(2)
print('We cannot identify uour problem you can either')
time.sleep(1)
print('Visit our store')
time.sleep(1)
print('Visit our website')

loop = input('Would you like to return to the start of the trouble-shooter?')
if loop == 'yes':
    problem()
else:
     exit()

Tags: intxtcloseinputyouriftimeexit
1条回答
网友
1楼 · 发布于 2024-04-26 15:12:40

你的问题都是一级缩进,所以不管用户回答什么都会被问到。如您的问题所述,如果某些问题只应在用户以“liquid”答复时提出,则这些问题应在“liquid”的[If]语句中。例如:

if "power" in problem:
    f = open('Power.txt', 'r')
    solution1 = f.readlines()
    print(solution1[0])
    f.close()
    print('Have you charged your phone overnight? if not do so')
    time.sleep(1)
    powerans = input('Does your phone turn on now?')

或者更好的是,在定义的函数中有问题,并在应用时调用这些函数

编辑:请注意,如果问题之间有依赖关系,最好使用不同的答案选项构建树状数据结构,并链接到适用于任一答案的文件和答案

相关问题 更多 >