0
Help With Random Numbers, Please!!!
Python - Example in comments So I am pretty new to programming. However, I’m trying to build a program for my daughter to help her learn math (very simple program) It produces 2 random numbers and asks her to add them together. If she gets it right she will get a congratulatory message. If not, she gets a message saying she was wrong. I am DEEPLY searching for a way to repeat the random number problem if she gets it wrong so she can try again. Any help out there?
8 Réponses
+ 1
# IAmJunior B try this in Pydroid or somewhere with a useful console where you can loop input...
from random import randint
def quiz():
x = randint(1,6)
y = randint(1,6)
answer = input(f'What does {x} + {y} = ?\n\t')
try:
while int(answer)!= x+y:
answer = input(f'Wrong. Try again:\n {x} + {y} = ?\n\t')
if int(answer)==x+y:
print(f'{x} + {y} = {answer} correct!')
quiz()
except:
print('Invalid input. Quitting...')
False
quiz()
+ 2
Bob_Li thanks a lot. I realized i didnt post my code after the fact but i appreciate your efforts a lot! I ended up figuring out what was wrong and now she can “go to work” with me when she comes home from preschool.
+ 1
Looking at your learning progress I assume you have got to learn the while...loop? if you have, then you can use that to make the repetition. And for the decision (whether she answered right or wrong) you can use `if` paired with an `else`
A few notes:
Since <x>, <y> and <z> are supposedly integers, you'd have to convert the input she gave into an integer also, before you can check whether <answer> equals <z>.
And you'd also need to provide a way for her to break out of the loop in case she wants to - maybe by giving zero as input. Otherwise the program will keep on repeating itself eternally.
+ 1
The problem is, there's no code to review, so nobody knows exactly what went wrong or where
0
#Not my real code just a quick example
For quick example:
X=randit(1,6)
Y=randit(1,6)
z= x + y
Answer = input(f”What does {x} + {y} = ?”)
#the rest of the code will just generate a new question whether right or wrong answer. If the answer is wrong, however, i want the last random one repeated to try again.
0
The problem is if she gets it right then yay, if wrong it wont repeat the same number just a different one
- 1
Hello
- 1
You willbe python