Logster
Logster

Reputation: 19

Function unable to change variable

I am trying to make a full-on guessing game with a shop that you can buy stuff with coins. but I had a function that was supposed to give the user a certain amount of coins depending on how many attempts it took them to guess the number. However, when I have a variable called 'coins' and when a player gets the number, I add coins to 'coins' it doesn't actually add coins. When I print 'coins' it still tells me 0. It's very confusing I know but I just want to fix this. I am using a mac with python 3, and am using two files, one for the main code, and the other for the functions. Do you see where I'm going wrong?

Main Code:

from guessing_functions import guess_game, guess_home

home = False
attempt = 0
coins = 0
print ("Atemps:Coins, 10:5, 7:10, 5:20, 3:40, 1:100 ")

guess_game(coins, attempt)

while not home:
    guess_home(coins)

Functions:

import random
def guess_game(coins, attempt):
    print ("This is a guessing game. ")
    found = False
    num = random.randint(1, 100)
    while not found:
        userGuess = input('Your Guess: ') ; userGuess = int(userGuess)
        if userGuess == num:
            print ("You got it!")
            found = True
        elif userGuess > num:
            print ("Guess Lower!")
        else:
            print ("Guess Higher")

        attempt += 1

        if attempt == 1 and found == True:
            print ("You won 100 coins!")
            coins += 100
        elif attempt == 2 and found == True:
            print ("You won 40 coins")
            coins += 40
        elif attempt == 3 and found == True:
            print ("You won 40 coins")
        elif attempt == 4 and found == True:
            print ("You won 20 coins")
            coins += 20
        elif attempt == 5 and found == True:
            print ("You won 20 coins")
            coins += 20
        elif attempt == 6 and found == True:
            print ("You won 10 coins")
            coins += 10
        elif attempt == 7 and found == True:
            print ("You won 10 coins")
            coins += 10
        elif attempt == 8 and found == True:
            print ("You won 5 coins")
            coins += 5
        elif attempt == 9 and found == True:
            print ("You won 5 coins")
            coins += 5
        elif attempt == 10 and found == True:
            print ("You won 5 coins")
            coins += 5

Upvotes: 0

Views: 278

Answers (3)

Erick Shepherd
Erick Shepherd

Reputation: 1443

To get it to work, you need only add return coins to the end of the guess_game function and collect the returned value in your main code as coins = guess_game(coins, attempt). However, if you'd like, you can simplify your code a little bit like so:

import random

def guessing_game(coins):

    print("This is a guessing game. ")
    attempts   = 0
    number     = random.randint(1, 100)
    user_guess = -number

    while user_guess != number:

        user_guess = int(input("Your Guess: "))

        if user_guess > number:

            print("Guess Lower!")

        elif user_guess < number:

            print("Guess Higher")

        else:

            print("You got it!")

            if attempts == 1:

                winnings = 100

            elif attempts in [2, 3]:

                winnings = 40

            elif attempts in [4, 5]:

                winnings = 20

            elif attempts in [6, 7]:

                winnings = 10

            elif attempts in [8, 9, 10]:

                winnings = 5

            else:

                winnings = 0

            print("You won {} coins!".format(winnings))

            return coins + winnings

        attempts += 1

With your main code as:

from guessing_functions import guessing_game

coins = 0
print("Starting balance: {} coins".format(coins))

print ("Winnings vs. Attempts: 10:5, 7:10, 5:20, 3:40, 1:100")
coins = guessing_game(coins)

print("Current balance: {} coins".format(coins))

Where the output from a sample run is as follows:

Starting balance: 0 coins
Winnings vs. Attempts: 10:5, 7:10, 5:20, 3:40, 1:100
This is a guessing game.
Your Guess: 50
Guess Lower!
Your Guess: 25
Guess Higher
Your Guess: 37
Guess Higher
Your Guess: 44
Guess Higher
Your Guess: 47
Guess Lower!
Your Guess: 46
You got it!
You won 20 coins!
Current balance: 20 coins

Upvotes: 0

Chen A.
Chen A.

Reputation: 11280

Your function uses coins in it's local scope. In order for the function to change the value of the outter scope (global) coins variable you need to explicity state that.

Add global coins inside your function before changing coins value.

coins = 0

def f():
    global coins
    coins = 5

f()
print coins
# 5

Or, an alternative way is to return coins value from the function, and call your function coins = guess_game(attempt)

Here is some useful resource for this subject

Upvotes: 1

wwii
wwii

Reputation: 23743

You should return the number of coins from the function and assign it to coins:

def guess_game(coins, attempt):
    ... # code to determine coin amount
    return coins

coins = guess_game(coins, attempt)

Defining Functions

Upvotes: 0

Related Questions