2014-10-13 57 views
-2

我想用隨機的水果字進行測驗,但我得到的問題總是相同的。這是爲什麼?從早期陳述循環

import random 
from random import shuffle 

fruit = ("banana", "apple", "Avocado", "Blackberry", "Coconut", "Durian", "Dragon Fruit", "Eggplant", "Feijoa") 

word = random.choice(fruit) 
shuffled = list(word) 
random.shuffle(shuffled) 
shuffled = ''.join(shuffled) 

qas = [(shuffled, word), 
    (shuffled, word), 
    (shuffled, word), 
] 

for question, rightAnswer in qas: 
    answer = raw_input(question + ' ') 
    if answer == rightAnswer: 
     print 'Right!' 
    else: 
     print 'No... the answer is ' + rightAnswer 
+0

如果腳本正常工作,問題會是什麼樣子? – John1024

回答

0

我假設你的意思是說你每次運行程序時,都會連續出現三個相同的問題。每次運行程序時都有一個不同的問題,每次運行重複三次。

在循環之前,您應在初始化後打印qas

qas = [(shuffled, word), 
     (shuffled, word), 
     (shuffled, word), 
] 
print qas 

你會看到,你有三個元組是相同的初始化它。

爲什麼你認爲qas的每個元素會有所不同?你把同樣的東西放進去......