Help me to turn this into a function and make this infinite. [School project] | Sololearn: Learn to code for FREE!

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.

8/1/2021 3:24:36 PM

Pol C.

39 Answers

New Answer

+5

import random While True: ... Your code

+5

This will help you with using SQLite: https://code.sololearn.com/cmTF25aA37Kk/?ref=app

+5

Who now gets the grade for this project, whose answer was marked as the best? What school do you attend? 😊

+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!

+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.

+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?

+2

Store in a file

+2

Why do you want to store a integer state with SQLite You could just write it in a file and read it later

+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

+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()

+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 👍

+2

It's because you put r.adjust_for_ambient_noise after r.listen

+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

+1

How would you put it?

+1

And now how could I stop it? Using break?

+1

import random score = 0 while True: Your_code stop = input("stop?") if stop == "yes": break else: continue

+1

But how could I do It? Using SQLite?

+1

I want to do that authomatically. It should not need to write it by hand.

+1

f = open('path_to_some_file', 'w') f.write(str(score)) f.close()

+1

you can also make it recursive