Python中randint函数与迭代循环问题:乘法题随机数重复
问题解决:循环中随机乘法题目重复的问题
你的问题出在随机数生成的位置——multi = randint(1,10)写在了for循环外面,只会在程序开始时生成一次随机数,后续循环全程复用这个值,所以每次都是同一个乘法题。
只需要把生成随机数的语句移到循环内部,让每次循环都重新生成一个新的随机数即可,修正后的代码如下:
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") score += 1 # 可以加上得分统计 else: print("Wrong !") # 循环结束后输出最终得分 print(f"Your final score: {score}/{nombreQuestions}")
内容的提问来源于stack exchange,提问作者KZ GLITCHER
相关产品推荐
相关产品推荐

