You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:15:45