0
最近,我一直在試圖編寫一個程序,它會向用戶提問,然後檢查答案。但有一個問題:更改變量不影響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 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()
每當我運行此,奇怪的是同樣的問題,並回答一次又一次運行,直到變量total
已經達到了4.當我在if語句寫道:print add2
,從1去如預期的那樣,3至5。但問題或答案不會改變。
謝謝馬克。你救了我很多麻煩。 –
如果這解決了您的問題,請接受答案。 – DavidG