Python帮助!模块工作不正常。如何使所有值相加?

Python帮助!模块工作不正常。如何使所有值相加?,python,module,Python,Module,所以我在Python上创建了一个程序,我的一个模块出现了问题。 这就是它看起来的样子 def提问(aQ): 当没有给出正确的输入时,它在重复问题的意义上起作用。但它不能相加……所以当我得到aQ时,它等于0,而它不应该等于。有人知道我做错了什么吗?请帮忙……你的问题不太清楚 如果可以保留格式,我会将此粘贴为注释,但这里有一个建议,说明如何减少重复代码,并希望在此过程中帮助您找到问题的根源 既然代码段只随数值变化,为什么不将其作为函数的参数呢。按照这些思路(不是实际运行,但您已经了解了): 我建议在

所以我在Python上创建了一个程序,我的一个模块出现了问题。 这就是它看起来的样子

def提问(aQ):


当没有给出正确的输入时,它在重复问题的意义上起作用。但它不能相加……所以当我得到aQ时,它等于0,而它不应该等于。有人知道我做错了什么吗?请帮忙……

你的问题不太清楚

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

既然代码段只随数值变化,为什么不将其作为函数的参数呢。按照这些思路(不是实际运行,但您已经了解了):


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

我最好的猜测是分数[0]…分数[8]不包含您认为它们包含的值


不清楚aQ值应该代表什么,但除非分数[0]…分数[8]包含唯一的值,否则每次只增加一个值是有意义的。

Hey Levon!我刚才有一个关于第二部分的简短问题。。。对于xrange(0,9)中的n:aQ=aQ+askQuestions(aQ,n)我应该把它放在哪里?类似于函数内部或主程序中。当我把它放在函数中时,它说“TypeError:Questions()正好接受2个参数(给定1个)”。抱歉,如果这些问题看起来很简单。我们必须在学校做一个编程作业:)@user2930414 for循环肯定不应该在askQuestion函数内部,而是在它外部。显示的缩进级别应该有效。。尽管完整的答案取决于查看所有代码。我将以上作为一个起点。只要能够将所有冗余代码减少到一个较短的代码段,管理起来就更容易了!我试过这么做。我还试着在每个while语句后打印aQ值,结果是零。分数[0]…分数[8]不包含唯一值。我基本上想做的是把所有的分数加起来(如果玩家回答“是”、“是”…),但当aQ返回时,它等于0。有没有想过我做错了什么?我认为这可能与设置answer=“nothing”进一步调试想法有关。在查看代码时,打印出分数[0]…分数[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[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
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)