mercredi 6 octobre 2021

random.randint not reinitializing in for loop

This is a simple newbie question. For each "i" iteration shouldn't we get a new "a" & "b" random variable. Why isn't a and b not being re-randomized with each loop iteration? I just don't see my error.

import random

for i in range(10):
    a = random.randint(1,12)
    b = random.randint(1,12)
    question = "What is "+str(a)+" x "+str(b)+"? "
    answer = input(question)
    if answer == a*b:
        print("Well done!")
    else:
        print("No.")

output
➜ python multiply.py
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!




Aucun commentaire:

Enregistrer un commentaire