gpt4 book ai didi

python - 随机陈述询问相同的陈述? Python

转载 作者:行者123 更新时间:2023-11-30 23:14:27 24 4
gpt4 key购买 nike

我一直在尝试做一个数学测验,先问一个随机问题,然后再问另一个随机问题,范围为 10 次,我的问题是,它会问一个随机问题,然后一次又一次地问同样的问题等等

import random

opList = ["+", "*", "-"]
numberofQuestions = 10
num1 = random.randint(0, 10)
num2 = random.randint(0, 10)
ops = random.choice(opList)
question = "%d %s %d" % (num1, ops, num2)
answer = eval(question)
totalScore = 0


childName = input("Enter your name: ")
print("Thank you for signing up for Arithmetic Quiz " + childName)
for i in range(numberofQuestions):
reply = int(input("What is " + question + " ? "))
if reply == answer:
print("Correct!!")
totalScore += 1
else:
print("Incorrect!!")

最佳答案

放置这些行:

num1 = random.randint(0, 10)
num2 = random.randint(0, 10)
ops = random.choice(opList)
question = "%d %s %d" % (num1, ops, num2)
answer = eval(question)

如果你有意味着他们只评估一次,为每个变量分配随机选择的静态值。

您需要将这些语句移动到 for 循环中,以便每次您需要新值时都会重新评估它们

关于python - 随机陈述询问相同的陈述? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28722311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com