从早期陈述循环

时间:2014-10-13 04:41:08

标签: python loops random

我想用随机的水果字母进行测验,但我得到的问题总是一样的。那是为什么?

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

1 个答案:

答案 0 :(得分:0)

我认为你的意思是每次运行程序时,你会得到三个相同的问题。每次运行程序时都会出现一个不同的问题,每次运行重复三次。

您应该在循环之前初始化后打印qas

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

您将看到已使用三个相同的元组初始化它。

为什么你认为qas的每个元素会有所不同?你把同样的东西放进去......