更改了不影响while循环的变量

时间:2016-08-03 08:01:58

标签: python-2.7 while-loop

最近,我一直在努力编写一个程序,向用户提问,然后检查答案。但是有一个问题:

item.Equals(default(KeyValuePair<string, ChangeRec>))

每当我运行它时,奇怪的是同样的问题和答案一次又一次地运行,直到变量get_questions=["What color is the daytime sky on a clear day? ", "blue", "What is the answer to life, the universe and everything? ", "42", "What is a three letter word for mouse trap? ", "cat", "What noise does a truly advanced machine make?","ping"] total=0 total2=0 good=0 def give_questions(): global get_questions global total2 global total global good add1=0 add2=1 question=get_questions[add1] answer=get_questions[add2] print while total != 4: print question print answer_given=raw_input("Answer: ") if answer_given != answer: print print "Wrong. The correct answer was",answer,"." print total2=total2+1 total=total+1 add1=add1+2 add2=add2+2 else: print print "Correct" print total2=total2+1 total=total+1 good=good+1 add1=add1+2 add2=add2+2 give_questions() 达到4.当我在if语句中写total时,它从1变为3到5,如预期的那样。但问题或答案不会改变。

1 个答案:

答案 0 :(得分:0)

你永远不会更新你的问题和答案,因为它们在你的'while'循环之外。因此,一直到你的循环,它会一直问你同样的问题。以下是在question = get_questions[add1]循环内移动answer = get_questions[add2]while的示例代码:

get_questions = ["What color is the daytime sky on a clear day? ", "blue",
                 "What is the answer to life, the universe and everything? ", "42",
                 "What is a three letter word for mouse trap? ", "cat",
                 "What noise does a truly advanced machine make?", "ping"]
total = 0
total2 = 0
good = 0

def give_questions():
    global get_questions
    global total
    global total2
    global good
    add1 = 0
    add2 = 1

    while total != 4:
        question = get_questions[add1]
        answer = get_questions[add2]

        print(question)
        answer_given = input("Answer:")
        if answer_given != answer:
            print ("Wrong. The correct answer was" ,answer)
            total2 += 1
            total += 1
            add1 += 2
            add2 += 2

        else:
            print ("Correct")
            total2 += 1
            total += 1
            good += 1
            add1 += 2
            add2 += 2

give_questions()

注意:您的代码中似乎有很多不必要的'print'语句似乎没有做任何事情。我已从上面的代码中删除它们以简化它。