I will admit I'm stuck on a school project right now.
I have defined functions that will generate random numbers for me, as well as a random operator (+, -, or *).
I have also defined a function that will display a problem using these random numbers.
I have created a program that will generate and display a random problem and ask the user for the solution. If the user is correct, the program prints 'Correct', and the opposite if the user is incorrect.
I have put all of this inside of a loop that will make it repeat 10 times. My issue is that I need it to generate 10 different problems instead of the same problem that it randomized the first time, 10 times.
Sorry for the weird wording.
*I am using python but am showing the code here using the CSS viewer because I couldn't get it to display any other way.
Thank you.
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')
Aucun commentaire:
Enregistrer un commentaire