Home > Enterprise >  Cannot see change of global variable changed from inside a function
Cannot see change of global variable changed from inside a function

Time:01-13

I'm new to python and coding in general. I'm trying to write a simple game that uses randomly generated Pokemon and abilities for a player and computer and whoever has the highest power wins.

The issue that I am facing is that I don't know how to get the score to update after each round without resetting back to 0. I want the game to continue infinitely until the player loses and add up a score each round.

Can anyone help? This is the current code:

import random
import requests

def random_pokemon():
    pokemon_number = random.randint(1, 151)
    url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(pokemon_number)
    response = requests.get(url)
    pokemon = response.json()
    return {
        'name': pokemon['name'],
        'Pokedex ID': pokemon['id'],
        'Height': pokemon['height'],
        'Weight': pokemon['weight'],
    }

def random_move():
    pokemon_move = random.randint(1,844)
    url = 'https://pokeapi.co/api/v2/move/{}/'.format(pokemon_move)
    response = requests.get(url)
    move = response.json()
    return {
       'name': move['name'],
       'power': move['pp'],
    }

score = 0

def add_to_score():
    global score
    score =  1

def current_score():
    global score

def run():

    player_pokemon = random_pokemon()
    print('Your Pokemon is {}'.format(player_pokemon['name']))
    player_move = random_move()
    print('Your move is {}'.format(player_move['name']))

    opponent_pokemon = random_pokemon()
    print('The opponents Pokemon is {}'.format(opponent_pokemon['name']))
    opponent_move = random_move()
    print('The opponents move is {}'.format(opponent_move['name']))

    player_stat = player_move['power']
    opponent_stat = opponent_move['power']

    print('Your Stat is {}'.format(player_stat))
    print('The opponents Stat is {}'.format(opponent_stat))

    if player_stat > opponent_stat:
        print('You Win! Congratulations!\n')
        print('Your score is {}'.format(add_to_score()))
        run()

    elif player_stat < opponent_stat:
        print('You Lose! Better Luck Next Time!')

    elif player_stat == 'None':
        print('You Lose! Better Luck Next Time!')

    elif opponent_stat == 'None':
        print('You Win! Congratulations!\n')
        print('Your score is {}'.format(add_to_score()))
        run()

    else:
        print('Its a Tie!\n')
        print('Your score is {}'.format(current_score()))
        run()



run()

I have tried including the score functions in the run() function but the outcome is still the same. Like I said i'm completely new to all of this so if someone could help it would be really appreciated!

CodePudding user response:

your add_to_score function has a typo and should be:

def add_to_score():
    global score
    score  = 1

Also, when printing the score you're not returning anything from the function that's why you will get None. I would suggest to print the score variable directly.

Full code:

import random
import requests

def random_pokemon():
    pokemon_number = random.randint(1, 151)
    url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(pokemon_number)
    response = requests.get(url)
    pokemon = response.json()
    return {
        'name': pokemon['name'],
        'Pokedex ID': pokemon['id'],
        'Height': pokemon['height'],
        'Weight': pokemon['weight'],
    }

def random_move():
    pokemon_move = random.randint(1,844)
    url = 'https://pokeapi.co/api/v2/move/{}/'.format(pokemon_move)
    response = requests.get(url)
    move = response.json()
    return {
       'name': move['name'],
       'power': move['pp'],
    }

score = 0

def add_to_score():
    global score
    score  = 1

def run():

    player_pokemon = random_pokemon()
    print('Your Pokemon is {}'.format(player_pokemon['name']))
    player_move = random_move()
    print('Your move is {}'.format(player_move['name']))

    opponent_pokemon = random_pokemon()
    print('The opponents Pokemon is {}'.format(opponent_pokemon['name']))
    opponent_move = random_move()
    print('The opponents move is {}'.format(opponent_move['name']))

    player_stat = player_move['power']
    opponent_stat = opponent_move['power']

    print('Your Stat is {}'.format(player_stat))
    print('The opponents Stat is {}'.format(opponent_stat))

    if player_stat > opponent_stat:
        print('You Win! Congratulations!\n')
        add_to_score()
        print('Your score is {}'.format(score))
        run()

    elif player_stat < opponent_stat:
        print('You Lose! Better Luck Next Time!')

    elif player_stat == 'None':
        print('You Lose! Better Luck Next Time!')

    elif opponent_stat == 'None':
        print('You Win! Congratulations!\n')
        add_to_score()
        print('Your score is {}'.format(score))
        run()

    else:
        print('Its a Tie!\n')
        print('Your score is {}'.format(score))
        run()

run()
  • Related