Help me to turn this into a function and make this infinite. [School project] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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
Pol C - avatar
38 Answers
+ 5
import random While True: ... Your code
1st Aug 2021, 3:45 PM
Abs Sh
Abs Sh - avatar
+ 5
This will help you with using SQLite: https://code.sololearn.com/cmTF25aA37Kk/?ref=app
1st Aug 2021, 4:06 PM
JaScript
JaScript - avatar
+ 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
JaScript - avatar
+ 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
Pol C - avatar
+ 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
JaScript - avatar
+ 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
Pol C - avatar
+ 2
Store in a file
1st Aug 2021, 3:55 PM
JaScript
JaScript - avatar
+ 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
Abs Sh - avatar
+ 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
Calvin Thomas - avatar
+ 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
Jannik Müller - avatar
+ 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
mycrochip - avatar
+ 2
It's because you put r.adjust_for_ambient_noise after r.listen
10th Aug 2021, 9:47 PM
Abs Sh
Abs Sh - avatar
+ 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
Abs Sh - avatar
+ 1
How would you put it?
1st Aug 2021, 3:43 PM
Pol C
Pol C - avatar
+ 1
And now how could I stop it? Using break?
1st Aug 2021, 3:48 PM
Pol C
Pol C - avatar
+ 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
Abs Sh - avatar
+ 1
But how could I do It? Using SQLite?
1st Aug 2021, 3:56 PM
Pol C
Pol C - avatar
+ 1
I want to do that authomatically. It should not need to write it by hand.
1st Aug 2021, 4:01 PM
Pol C
Pol C - avatar
+ 1
f = open('path_to_some_file', 'w') f.write(str(score)) f.close()
1st Aug 2021, 4:03 PM
Abs Sh
Abs Sh - avatar
+ 1
you can also make it recursive
1st Aug 2021, 4:05 PM
Rellot's screwdriver
Rellot's screwdriver - avatar