0

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
asked Oct 25, 2017 at 19:15
3
  • What type of exception are you getting? Commented Oct 25, 2017 at 19:15
  • When I print 'coins' it tells me 0 even after I changed the value in the function Commented Oct 25, 2017 at 19:17
  • define coins as global in your function Commented Oct 25, 2017 at 19:23

3 Answers 3

1

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

answered Oct 25, 2017 at 19:25
Sign up to request clarification or add additional context in comments.

Comments

0

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
answered Oct 25, 2017 at 21:05

3 Comments

why did you use 'winnings', how does that match up with coins?
@Logster Because when playing games of chance, if you walk away with a positive sum, it's called your "winnings". I replaced the incrementation of coins with winnings so that winnings could be printed without having to type ten different print strings. The new value of coins is the starting value plus the winnings, thus return coins + winnings.
@Logster No problem! If this answered your question, don't forget to tap the "accept answer" button.
0

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

answered Oct 25, 2017 at 19:26

2 Comments

when I do this it tells me 'coins' is not defined.
@Logster - in your post it looked like you defined coins, coins = 0, before calling the function. I assumed you would still do that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.