我会承认我现在正在坚持学校项目。
我定义了为我生成随机数的函数,以及一个随机运算符(+, - 或*)。
我还定义了一个使用这些随机数显示问题的函数。
我创建了一个程序,它将生成并显示一个随机问题,并要求用户提供解决方案。如果用户是正确的,程序将打印“正确”,如果用户不正确,则相反。
我把所有这些放在一个循环中,使它重复10次。我的问题是我需要它来产生10个不同的问题,而不是第一次随机化10次的相同问题。
对于这种奇怪的措辞感到抱歉。
*我正在使用python,但我在这里使用CSS查看器显示代码,因为我无法让它显示任何其他方式。
谢谢。
import random
max = 10
def getOp(max): #generates a random number between 1 and 10
randNum = random.randint(0,max)
return randNum
randNum = getOp(max)
def getOperator(): #gets a random operator
opValue = random.randint(1,3)
if opValue == 1:
operator1 = '+'
elif opValue == 2:
operator1 = '-'
elif opValue == 3:
operator1 = '*'
return operator1
operand1 = getOp(max)
operand2 = getOp(max)
operator = getOperator()
def doIt(operand1, operand2, operator): #does the problem so we can verify with user
if operator == '+':
answer = operand1 + operand2
elif operator == '-':
answer = operand1 - operand2
elif operator == '*':
answer = operand1 * operand2
return answer
answer = doIt(operand1, operand2, operator)
def displayProblem(operand1, operand2, operator): #displays the problem
print(operand1, operator, operand2, '=')
###My program:
for _ in range(10): #loops the program 10 times
displayProblem(operand1, operand2, operator)
userSolution = int(input('Please enter your solution: '))
if userSolution == doIt(operand1, operand2, operator):
print('Correct')
elif userSolution != doIt(operand1, operand2, operator):
print('Incorrect')
答案 0 :(得分:2)
只需将生成随机值的代码移动到for循环中:
for _ in range(10): #loops the program 10 times
randNum = getOp(max)
operand1 = getOp(max)
operand2 = getOp(max)
operator = getOperator()
answer = doIt(operand1, operand2, operator)
displayProblem(operand1, operand2, operator)
userSolution = int(input('Please enter your solution: '))
if userSolution == doIt(operand1, operand2, operator):
print('Correct')
elif userSolution != doIt(operand1, operand2, operator):
print('Incorrect')
每当你要求用户输入时,就会调用它。
答案 1 :(得分:1)
您生成问题,然后在循环中显示10次:
generateProblem()
for _ in range(10):
showProblem()
当然你会得到10次同样的问题。要解决此问题,请在循环中生成问题:
for _ in range(10):
generateProblem()
showProblem()
答案 2 :(得分:0)
破坏你的代码
import random
max = 10
def getOp(max): #generates a random number between 1 and 10
randNum = random.randint(0,max)
return randNum
def getOperator(): #gets a random operator
opValue = random.randint(1,3)
if opValue == 1:
operator1 = '+'
elif opValue == 2:
operator1 = '-'
elif opValue == 3:
operator1 = '*'
return operator1
def doIt(operand1, operand2, operator): #does the problem so we can verify with user
if operator == '+':
answer = operand1 + operand2
elif operator == '-':
answer = operand1 - operand2
elif operator == '*':
answer = operand1 * operand2
return answer
def displayProblem(operand1, operand2, operator): #displays the problem
print(operand1, operator, operand2, '=')
###My program:
for _ in range(10): #loops the program 10 times
operand1 = getOp(max)
operand2 = getOp(max)
operator = getOperator()
displayProblem(operand1, operand2, operator)
userSolution = int(input('Please enter your solution: '))
if userSolution == doIt(operand1, operand2, operator):
print('Correct')
elif userSolution != doIt(operand1, operand2, operator):
print('Incorrect')