Как добавить систему точек в цикл, которая продолжает отслеживать точки после перезапуска цикла?
I made a number guessing with a loop that restarts. But when the loop restarts the score system gets reset to 0 as I have set a variable to track score and on loop it is first set to 0 then using `score=score+1` I can track score but when it loops it just becomes 0 then gets set to 1? I've tried taking the score out of the loop but it comes back with a `local variable 'score' referenced before assignment`.
<pre> from random import * yesList = ("yes", "yeah", "sure", "definitely", "y", "ye", "yeah") def win(): print("Correct") print("You have won", Pname + "!") print("I've added 1 point to your score!") score = int("0") score= score+1 print("You've scored", score, "points!") restart() Pname = input("What is your name? ") print("Hello", Pname, "let's play a game.") def restart(): restart=input("Do you want to play again? ").lower() if restart in yesList: main() else: print("Game Over") print("You scored", score, "points!") exit() def main(): rnum = randint(1, 100) num1 = int(input("Guess a number between 1 and 100: ")) if num1 == rnum: win() elif num1>rnum: print("Too High") else: print("Too Low") num2 = int(input("Guess again: ")) if num2==rnum: win() elif num2>rnum: print("Too High") else: print("Too Low") num3 = int(input("Guess again: ")) if num3==rnum: win() elif num3>rnum: print("Too High") else: print("Too Low") num4 = int(input("Guess again: ")) if num4==rnum: win() elif num4>rnum: print("Too High") else: print("Too Low") num5 = int(input("Guess again: ")) if num5==rnum: win() elif num5>rnum: print("Too High") else: print("Too Low") num6 = int(input("Guess again: ")) if num6==rnum: win() elif num6>rnum: print("Too High") else: print("Too Low") num7 = int(input("Guess again: ")) if num7==rnum: win() elif num7>rnum: print("Too High") else: print("Too Low") str(print("The answer is", rnum)) restart() score=0 main()
Что я уже пробовал:
Traceback (most recent call last): File "program.py", line 115, in <module> main() File "program.py", line 40, in main win() File "program.py", line 14, in win score= score+1 UnboundLocalError: local variable 'score' referenced before assignment This is the error message that comes when you take the score out of the loop.