-1

所以我在 Python 上创建了一个程序,但我的一个模块有问题。这就是它的样子……

定义询问问题(aQ):

answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[0]
    answer = input(Questions[1])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[1]
    answer = input(Questions[2])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[2]
    answer = input(Questions[3])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[3]
    answer = input(Questions[4])
answer = "nothing"


while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[4]
    answer = input(Questions[5])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[5]
    answer = input(Questions[6])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[6]
    answer = input(Questions[7])
answer = "nothing"


while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[7]
    answer = input(Questions[8])
answer = "nothing"

while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
    if answer in("Yes", "yes", "Y", "y", "YES"):
        aQ = aQ + Score[8]
    answer = input(Questions[9])
return aQ

当没有给出适当的输入时,它在重复问题的意义上起作用。但它并没有加起来......所以当我得到 aQ 时它等于 0,而它不应该是。有谁知道我做错了什么?请帮忙...

4

2 回答 2

0

你的问题不是很清楚。

如果我可以保留格式,我会将其粘贴为评论,但这里有一个关于如何减少重复代码的建议,并希望在此过程中帮助您找到问题的根源。

由于您的代码段仅因数值而异,为什么不将其作为函数的参数。这些方面的东西(实际上没有运行,但你明白了):

def askQuestions(aQ, i):
    answer = "nothing"

    while answer not in("Yes", "yes", "Y", "y", "YES", "No", "no", "N", "n", "NO"):
        if answer in("Yes", "yes", "Y", "y", "YES"):
            aQ = aQ + Score[i]
        answer = input(Questions[i+1])
    return aQ

for n in xrange(0, 9):
    aQ = aQ + askQuestions(aQ, n)
于 2013-10-29T03:10:55.910 回答
0

我建议在开始时初始化 aQ 并打印调试值。

我最好的猜测是 Score[0]...Score[8] 不包含您认为的值。

目前尚不清楚 aQ 应该代表什么值,但除非 Score[0]...Score[8] 包含唯一值,否则每次只增加 1 是有意义的。

于 2013-10-29T03:11:58.740 回答