-1
import random
game = False
random_num = random.randint(1 , 100)
print(random_num)
x = 0
if x == 1:
    game = True
def code():
    if number == random_num :
        x + 1
        print('you von')

    elif number > random_num :
        print('lov')
    elif number < random_num :
        print('higher')

if game == True:
    print ("game over")
while x == 0:
    number = int(input("Guess the number "))
    code()

code should end if the correct number is guessed game over is never printed keeps asking me to guess the number

i also tried this code it keeps failing pycharm greys out-> game in if function game = True

import random
game = False
random_num = random.randint(1 , 100)
print(random_num)
#x = 0
#if x == 1:
    #game = True
def code():
    if number == random_num :
        game = True
        print('you von')

    elif number > random_num :
        print('lov')
    elif number < random_num :
        print('higher')

if game == True:
    print ("game over")
while game == False:
    number = int(input("Guess the number "))
    code()
noob
  • 33
  • 6
  • 1
    use `print()` to see values in variables in different moments. OR learn how to use read debuger. You never change `game` to `True` so it can't execute `print ("game over")` - beside your code seems in wrong order - you check `if game == True:` before you even start game - and later you don't check it. OR maybe you have wrong indentation and ` `if game == True:` should be inside `def code` ? – furas Dec 12 '20 at 01:51
  • 1
    "game over is never printed " Talk me through the code, step by step, and try to convince me that it should be printed. Right now I think it definitely should not be, and I don't understand why you think it should be. I can't help you understand the problem unless I understand your thinking. – Karl Knechtel Dec 12 '20 at 01:53

2 Answers2

1

That's because you're trying to change the game variable, which is a global variable. It lives outside the scope of the code function.

In order to achieve what you want, you have to declare game as global at the beginning of the code() function. Otherwise, it will take game as fresh local variable:

def code():
    global game
    [...]

I found this related question that you may find useful: Python function global variables?

From the python programming FAQ:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Turtlean
  • 579
  • 4
  • 9
1

So there are a couple of things here, but the most important is that the variable x is not being returned with the new value if we guess correctly. In order to return a variable from a function with an updated variable, it needs to be returned, like so:

def code(x):
    if number == random_num :
        x = 1
        print('you von')

    elif number > random_num :
        print('lov')
    elif number < random_num :
        print('higher')
    return x

while x == 0:
    number = int(input("Guess the number "))
    x = code(x)
    print(x)

The order of the statements also means that some of them are getting skipped. When we reorder like this:

import random
game = False
random_num = random.randint(1 , 100)
print(random_num)
x = 0

def code(x,number):
    if number == random_num :
        x = 1
        print('you von')
    elif number > random_num :
        print('lov')
    elif number < random_num :
        print('higher')
    return x

while x == 0:
    number = int(input("Guess the number "))
    x = code(x,number)
    print(x)

if x == 1:
    game = True
if game == True:
    print ("game over")

We get the desired output and the program exits when the number is guessed.