【问题标题】:More Efficient Way of List Function and Questioning/Answering? [closed]更有效的列表功能和提问/回答方式? [关闭]
【发布时间】:2016-05-16 10:27:11
【问题描述】:

我正在为我的 GCSE 做一个疑难解答程序,它会根据用户的输入给出适当的答案。我原来的代码是:

keywords = ["k1","k2","k3","k4","k5","k6","k7","k8","k9","kk"]

question_about_phone = input("What Seems to be the Problem? Please be percific but don't bombard us with too much info").lower()

file = open('code.txt','r')
solution = [line.strip(',') for line in file.readlines()]

for x in range(0, 10):
    if keywords[x] in question_about_phone:
        print(solution[x])

然而发生的事情是我的老师告诉我我不能堆叠解决方案,例如k1 和 k3 不能最终合并 k1 和 k3 解决方案,但必须放置一个单独的解决方案。

因此,这就是我所做的:

keywords = ["k1","k2","k3","k4","k5","k6","k7","k8","k9","kk"]
true = ["False","False","False","False","False","False","False","False","False","False"]
question_about_phone = input("What Seems to be the Problem? Please be percific but don't bombard us with too much info").lower()
file = open('code.txt','r')
solution = [line.strip(',') for line in file.readlines()]

for x in range(0, 10):
    if keywords[x] in question_about_phone:
        true == "true"

if true[1] == "true" and true[2] == "true" and true[3] == "true":
    print(solution[1])

if true[1] == "true" and true[3] == "true" and true[5] == "true":
    print(solution[2])

if true[1] == "true" and true[7] == "true" and true[8] == "true":
    print(solution[3])

if true[8] == "true" and true[4] == "true" and true[5] == "true":
    print(solution[4])

if true[6] == "true" and true[9] == "true" and true[4] == "true":
    print(solution[5])

if true[3] == "true" and true[4] == "true" and true[8] == "true":
    print(solution[6])

if true[1] == "true" and true[9] == "true" and true[2] == "true":
    print(solution[7])

if true[10] == "true" and true[1] == "true" and true[9] == "true":
    print(solution[8])

if true[3] == "true" and true[7] == "true" and true[10] == "true":
    print(solution[9])

if true[7] == "true" and true[3] == "true" and true[4] == "true":
    print(solution[10])

现在有什么方法可以简化这个,还是我必须这样。非常感谢,谢谢

【问题讨论】:

  • 附注:我认为您的意思是 please be specific 而不是 please be percific
  • 您应该在CodeReview 询问。在那里,格式将特别适合您的问题,您很可能会得到更好的帮助。
  • 不要从问题中删除内容,而应该删除问题。虽然,如果 Darth Kotik 的回答有帮助,你应该接受它
  • @Kay - 我一点也不,如果问题不再相关,那么他们应该删除它,但是因为这是试图“把他们的球带回家”我已经编辑了我的评论。
  • 在 MSO 讨论过:User keeps deleting own question

标签: python list dictionary


【解决方案1】:

我认为可以使用一些字典来存储解决方案:

solutions = {
    solution[1]: [1, 2, 3], 
    solution[2]: [1, 3, 5],
    solution[3]: [1, 7, 8], 
    ...
}

所以你可以做这样的事情

for answer, questions in solutions.items():
    if all([true(x)=="true" for x in questions]):
        print answer
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多