0

# Help me to turn this into a function and make this infinite. [School project]

Hey everyone, I need some help for my school project. I need to create a program that generates random numbers, shows them to you, and then asks you the result of the sum. If the answer is correct, it adds some points to the score, and if you fail, your score will remain the same, but another sum will appear. Then, I should be able to choose if I want to do the sum or If I want to exit the program. Here is my code: def Funcio1(): # Variables puntuaciΓ³ = 0 usuari = str(input("Com et dius? ")) edat = str(input("Quina edat tens? ")) while True: x = random.randint(0,10) y = random.randint(0,10) z = x + y print (str(x) + "+" + str(y)) resultat = int (input()) if resultat == z: print ("Correcte") puntuaciΓ³ = puntuaciΓ³ + 5 print ("Tens aquests punts:", puntuaciΓ³) else: if resultat != z: print ("Malament!") parar = input("Vols parar? ") if parar == ("si" ): print (usuari + ", has aconseguit " + str (puntuaciΓ³) + " punts") break else: continue Sorry for the quality of the code, it's my first big projecte since I started learning python a month ago.

1st Aug 2021, 3:24 PM
Pol C
+ 5
import random While True: ... Your code
1st Aug 2021, 3:45 PM
Abs Sh
+ 5
1st Aug 2021, 4:06 PM
JaScript
+ 5
Who now gets the grade for this project, whose answer was marked as the best? What school do you attend? π
2nd Aug 2021, 10:46 AM
JaScript
+ 5
Hey everyone. Thank you for helping me to do this task. The teacher told me that I did a pretty good job so far. However, I Will be probably needing your help in the future. The teacher told us to keep upgrading the program (using images, audios...) and this a new thing for me. I hope I can rely on you!
3rd Aug 2021, 2:11 PM
Pol C
+ 4
You can do that infinite with: while True: #your code if βinputβ == βendβ: print("Breaking") break elif βinputβ == βaddβ: #your code for addition etc.
1st Aug 2021, 3:52 PM
JaScript
+ 2
Alright. Thank you for helping me. If I wanted to close the program, and then open it in another time, how could I keep the score?
1st Aug 2021, 3:54 PM
Pol C
+ 2
Store in a file
1st Aug 2021, 3:55 PM
JaScript
+ 2
Why do you want to store a integer state with SQLite You could just write it in a file and read it later
1st Aug 2021, 3:59 PM
Abs Sh
+ 2
Pol CaΓ±adas Costa Here's the completed version of your code (also slightly modified by me): score = 0 user = input("What's your name?\n") while True: x = random.randint(0,10) y = random.randint(0,10) sum = x + y print (f"{x} + {y}") if int(input()) == sum: print("Correct\nTens aquests punts:", score) score += 5 else: print("Malament!\nWould you like to stop?") if input("[Y/N]: ") == "Y": break print(f"{user}, you have scored {score} points") # Hope this helps
2nd Aug 2021, 6:05 PM
Calvin Thomas
+ 2
Addition to the Comment by Abs Sh: To read the score again, run f = open("your_file", "r") score = int(f.read()) f.close()
3rd Aug 2021, 2:43 PM
Jannik MΓΌller
+ 2
Just wrap your program in a while loop thus: while True: program codes decision = input() if decision == "e" break #This stops the loop else: continue π
5th Aug 2021, 5:02 PM
mycrochip
+ 2
It's because you put r.adjust_for_ambient_noise after r.listen
10th Aug 2021, 9:47 PM
Abs Sh
+ 1
Put all of it in "while True:" loop as for stopping you can ask the user with input inside the while loop and if you want to stop just run the "break" command
1st Aug 2021, 3:42 PM
Abs Sh
+ 1
How would you put it?
1st Aug 2021, 3:43 PM
Pol C
+ 1
And now how could I stop it? Using break?
1st Aug 2021, 3:48 PM
Pol C
+ 1
import random score = 0 while True: Your_code stop = input("stop?") if stop == "yes": break else: continue
1st Aug 2021, 3:52 PM
Abs Sh
+ 1
But how could I do It? Using SQLite?
1st Aug 2021, 3:56 PM
Pol C
+ 1
I want to do that authomatically. It should not need to write it by hand.
1st Aug 2021, 4:01 PM
Pol C
+ 1
f = open('path_to_some_file', 'w') f.write(str(score)) f.close()
1st Aug 2021, 4:03 PM
Abs Sh
+ 1
you can also make it recursive
1st Aug 2021, 4:05 PM
Rellot's screwdriver