获取代码以在一个变量

时间:2017-02-24 23:13:28

标签: python

我正在制作一个程序,这是一个手机故障排除器,可以识别关键字,然后根据所述关键字读取并打印出有解决方案的文档。现在用我的代码我问问题是什么,如果我说液体我想要液体文件打印和一系列与液体问题有关的问题。现在,当我这样做时,它一直在问第一组问题是权力。任何人都可以帮助我,以便找到正确的问题串吗?

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

1 个答案:

答案 0 :(得分:0)

您的问题都是第一级缩进,因此无论用户回答什么,他们都会被问到。如果,正如您的问题所述,只有当用户回复“液体”时才应询问某些问题,那么这些问题应该在[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?')

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

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