1
This number guessing game script doesn’t work, in part if z = "s" Python says the syntax is invalid. Someone could tell me why it doesn’t work, and what would be the right one?
import random
def rerun():
    if z = "s"
        numero_aleatorio= random.randint(1,10)
        x = numero_aleatorio
        y = input("Advinhe o numero ")
        y = int(y)
        if y > x:
            y = int(input("Numero muito alto! "))
            while y > x:
                y = int(input("Numero muito alto! "))
                if y == x:
                    print("Voce advinhou!")
                    z = input("Você quer continuar jogando?(s/n) ")
                elif y < x:
                y = int(input("Numero muito baixo! "))
            while y < x:
                y = int(input("Numero muito baixo! "))
                if y == x:
                    print("Voce advinhou!")
                    z = input("Você quer continuar jogando?(s/n) ")
                     if z == "s":
                         rerun()
                     if z == "n":
                         quit()
numero_aleatorio= random.randint(1,10)
x = numero_aleatorio
y = input("Advinhe o numero ")
y = int(y)
if y > x:
    y = int(input("Numero muito alto! "))
    while y > x:
            y = int(input("Numero muito alto! "))
            if y == x:
                print("Voce advinhou!")
                z = input("Você quer continuar jogando?(s/n) ")
                    rerun()
elif y < x:
    y = int(input("Numero muito baixo! "))
    while y < x:
            y = int(input("Numero muito baixo! "))
            if y == x:
                print("Voce advinhou!")
                z = input("Você quer continuar jogando?(s/n) ")
                    rerun()
I also tried with the code like this:
import random
def rerun():
    if z == "s":
        numero_aleatorio= random.randint(1,10)
        x = numero_aleatorio
        y = input("Advinhe o numero ")
        y = int(y)
        if y > x:
            y = int(input("Numero muito alto! "))
            while y > x:
                y = int(input("Numero muito alto! "))
                if y == x:
                    print("Voce advinhou!")
                    z = input("Você quer continuar jogando?(s/n) ")
                elif y < x:
                    y = int(input("Numero muito baixo! "))
            while y < x:
                y = int(input("Numero muito baixo! "))
                if y == x:
                    print("Voce advinhou!")
                    z = input("Você quer continuar jogando?(s/n) ")
                    if z == "s":
                         rerun()
                    if z == "n":
                         quit()
numero_aleatorio= random.randint(1,10)
x = numero_aleatorio
y = input("Advinhe o numero ")
y = int(y)
if y > x:
    y = int(input("Numero muito alto! "))
    while y > x:
            y = int(input("Numero muito alto! "))
            if y == x:
                print("Voce advinhou!")
                z = input("Você quer continuar jogando?(s/n) ")
                rerun()
elif y < x:
    y = int(input("Numero muito baixo! "))
    while y < x:
            y = int(input("Numero muito baixo! "))
            if y == x:
                print("Voce advinhou!")
                z = input("Você quer continuar jogando?(s/n) ")
                rerun()
But now gives the error below, after user select whether to continue playing or not:
Traceback (most recent call last):
  File "C:/Users/chlav/Documents/Python scripts/advinhar.py", line 45, in <module>
    rerun()
  File "C:/Users/chlav/Documents/Python scripts/advinhar.py", line 3, in rerun
    if z == "s":
UnboundLocalError: local variable 'z' referenced before assignment
Lacked a
:at the end of the line that is giving error, and as is a comparison, need to==...– AlexCiuffa
Yet you still make the same mistake...
– Carlos Henrique
Carlos Henrique the correct would be
if z == 's':in the third line of your script.– ThiagoO
Python
=is assignment, for equality test use==.– ThiagoO
I fixed it and some paragraphs that were giving error and now it was like this
– Carlos Henrique
Another error appeared?
– ThiagoO
Yes, I’ve edited the post
– Carlos Henrique
avoids the use of global variables... draws the comparison from within the function and plays it for the global scope... you are using the same global variable z within the function and making a comparison hoping that it is the global z. but since you have another z within the scope of the function it tries to take the local z and a syntax error
– Gabriel Belini
But then how do I restart the game?
– Carlos Henrique
A repeat loop would have to be used.
– ThiagoO
Google Scope of Variables in Python and you’ll understand your code error
– Gabriel Belini