Help With Random Numbers, Please!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

25th Sep 2022, 10:22 PM
IAmJunior B
IAmJunior B - avatar
8 Answers
+ 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()
27th Sep 2022, 12:57 PM
Bob_Li
Bob_Li - avatar
+ 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.
28th Sep 2022, 9:02 PM
IAmJunior B
IAmJunior B - avatar
+ 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.
26th Sep 2022, 12:38 AM
Ipang
+ 1
The problem is, there's no code to review, so nobody knows exactly what went wrong or where
26th Sep 2022, 4:42 AM
Ipang
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.
25th Sep 2022, 11:20 PM
IAmJunior B
IAmJunior B - avatar
0
The problem is if she gets it right then yay, if wrong it wont repeat the same number just a different one
26th Sep 2022, 1:09 AM
IAmJunior B
IAmJunior B - avatar
- 1
Hello
27th Sep 2022, 3:25 PM
Nodirbek NodirbekMavlonov
Nodirbek NodirbekMavlonov - avatar
- 1
You willbe python
27th Sep 2022, 3:25 PM
Nodirbek NodirbekMavlonov
Nodirbek NodirbekMavlonov - avatar