2017-10-14 86 views
-2

我只完成了1個學期的python,但是這看起來應該是工作而且我完全沒有想法!基本上,我需要創建一個10問題測驗,要求按隨機順序的問題,我面對的問題是,僅環承認第一次的問題,而不是通過對回答所有10我的if語句不能在Python中正確循環使用

import random 
questions = ["2x2", "4x4", "3x3", "5x5", "0x100", "6x2", "10x10", "5x7", 
"9x8" ,"9x9"] 
questions2 = ["Question 1", "Question 2", "Question 3", "Question 4", 
"Question 5", "Question 6", "Question 7", "Question 8", "Question 9" 
,"Question 10"] 
num1 = [2, 4, 3, 5, 0, 6, 10, 5, 9, 9] 
num2 = [2, 4, 3, 5, 100, 2, 10, 7, 8, 9] 
ans = [4, 16, 9, 25, 0, 12, 100, 35, 75, 81] 
random.randint(0,9) 
random.shuffle(questions) 
question2 = 0 
question = 0 
while question < 10: 
    print ((questions2[question]), (questions[question])) 
    user_typed_ans= int(input()) 
    if user_typed_ans==num1[0]* num2[0]: 
     print ("Correct") 
    else: 
     print ("Incorrect:") 
     print(ans[0]) 
    question += 1 
+1

那麼,是不是'[0]'看起來可疑?如果你想要其他的值,不要總是使用第一個索引 –

+0

另外,而不是'questions2',你爲什麼不使用'print(「問題」+問題)'? –

+0

它爲我循環。當然,它並沒有承認我的正確答案。 – toonarmycaptain

回答

0

使用正確的指數移動檢查:

import random 

questions = ["2x2", "4x4", "3x3", "5x5", "0x100", "6x2", "10x10", "5x7", "9x8" ,"9x9"] 
questions2 = ["Question 1", "Question 2", "Question 3", "Question 4", 
"Question 5", "Question 6", "Question 7", "Question 8", "Question 9" 
,"Question 10"] 
num1 = [2, 4, 3, 5, 0, 6, 10, 5, 9, 9] 
num2 = [2, 4, 3, 5, 100, 2, 10, 7, 8, 9] 
ans = [4, 16, 9, 25, 0, 12, 100, 35, 75, 81] 

# Comment this for correct answers 
# random.shuffle(questions) 

question = 0 
while question < 10: 
    print ((questions2[question]), (questions[question])) 
    user_typed_ans = int(input()) 
    if user_typed_ans == num1[question] * num2[question]: 
     print ("Correct") 
    else: 
     print ("Incorrect:") 
     print (ans[question]) 
    question += 1 
+0

我主要工作,但現在它重複同樣的問題,我只需要問他們一次。 –

+0

刪除'random.shuffle(問題)'(或註釋它),以獲得正確的序列。 – SatanDmytro