Home > Back-end >  Error with randint and iterative loop in python
Error with randint and iterative loop in python

Time:10-09

i'm a beginner in python and I am coding a multiplicator challenge in terminal. I need to call the function randint in order to ask random questions about multiplication tables. The problem is that in my iterative loop, the number printed is always the same, it generates a random one but it is the same in every loop, that's a big problem for me, here's my code

from random import *

nombreQuestions = int(input("How many questions ? : "))
score = 0
choixTable= int(input("On which number do you want to get asked on ? : "))
multi=(randint(1,10))

for nombreQuestions in range(nombreQuestions):
    question=str(choixTable) " x " str(multi) " = "
    reponse = int(input(question))

    if reponse == choixTable*multi:
       print("Well played")
    else:
       print("Wrong !")

Thank you very much

CodePudding user response:

You only call randint at the start at line 6 where it is set once before you enter the loop This means that each iteraction uses the same value To fix this you need to set multi inside the loop (instead of outside)

from random import *

nombreQuestions = int(input("How many questions ? : "))
score = 0
choixTable= int(input("On which number do you want to get asked on ? : "))

for nombreQuestions in range(nombreQuestions):
    multi=(randint(1,10)) # multi is now set every loop
    question=str(choixTable) " x " str(multi) " = "
    reponse = int(input(question))

    if reponse == choixTable*multi:
       print("Well played")
    else:
       print("Wrong !")

CodePudding user response:

Add multi=... in the loop so it will execute on every loop Iteration.

from random import *

nombreQuestions = int(input("How many questions ? : "))
score = 0
choixTable= int(input("On which number do you want to get asked on ? : "))


for nombreQuestions in range(nombreQuestions):
    multi=(randint(1,10))
    question=str(choixTable) " x " str(multi) " = "
    reponse = int(input(question))

    if reponse == choixTable*multi:
       print("Well played")
    else:
       print("Wrong !")

CodePudding user response:

If you want to generate a random number each time then you need to call the randint() method inside the loop not outside.

Which mean this line multi=(randint(1,10)) should be used inside for loop.

Just like this,

from random import *

nombreQuestions = int(input("How many questions ? : "))
score = 0
choixTable= int(input("On which number do you want to get asked on ? : "))

for nombreQuestions in range(nombreQuestions):
    multi = randint(1,10)
    question=str(choixTable) " x " str(multi) " = "
    reponse = int(input(question))

    if reponse == choixTable*multi:
       print("Well played")
    else:
       print("Wrong !")

One thing more, specifying the name of the function in the import statement is more preferred i.e., instead of importing like from random import * use from random import randint

  • Related