-1

Absolute beginner here, trying to make a simple coin toss game to practice the first couple of chapters I've read. I tried to add a score board to the game but the score is constantly stuck at 0 to 0. also any other tips would be appreciated as well. I don't know anyone that programs so I could use the advice.

  """This will be an attempt to create a simple 
coin toss game in which the player chooses heads or
tails and a random generator will pick one from a list
of either heads or tails and return whether or not 
the player chose correctly. I also hope to
create a scoreboard"""

from random import randint

coin = ['heads' , 'tails']
side = coin[0]
guess = 'heads'
token = int(raw_input('how many games?'))
wins = 0
losses = 0


def getinfo(token):
    guess = raw_input('Heads or Tails?')

def flipcoin(guess, side, wins, losses):
    side = coin[randint(0,1)]
    print 'You Chose: %s' %(guess)
    print 'Coin Is: %s' %(side)
    if guess == side:
        print 'you win'
        wins = wins + 1
    else:
        print 'you lose'
        losses = losses + 1

def main(token):
    while token > 0:
        getinfo(token)
        flipcoin(guess, side, wins, losses)
        if wins > losses:
            print "You are winning %s to %d" %(wins , losses)
        elif wins == losses:
            print "You are tied %s to %d" %(wins , losses)
        else:
            print "You are losing %s to %d" %(losses, wins)
        token -= 1
        print '%d games left' %(token)
        print 'wins: %s' %(wins)
        print 'losses: %s' %(losses)

main(token)
admdrew
  • 3,790
  • 4
  • 27
  • 39
  • 1
    Your functions are returning anything. – Andy Jan 05 '15 at 20:12
  • Change `getinfo(token)` in `main` to `guess = getinfo(token)` and `return raw_input('Heads or Tails?')` in `getinfo()`... – Mr. Polywhirl Jan 05 '15 at 20:14
  • @Anthony Sidoti, I think you are getting down-voted because people don't think your question was clear enough. Your question seemed pretty clear to me ("Why are my `wins` and `losses` variables stuck at 0?") but you didn't spell it out explicitly. Also, StackOverflow seems to collectively hate requests for "any other tips"; there is another board, http://codereview.stackexchange.com/, where general requests to have your code reviewed are welcome. So people on StackOverflow downvote you to punish you for posting a code review request here. – steveha Jan 05 '15 at 20:24
  • I appreciate that, it's my first post – tonysamurai Jan 05 '15 at 20:34

3 Answers3

1

Your problem is that the variables are declared at "module scope", outside the functions, and then you try to modify them from inside the functions.

Instead of modifying the module-scope variables, the functions are creating their own local variables.

One way to solve the problem is to add global declarations for the variables.

Use of "global" keyword in Python

Another, possibly better, way is to put the variables in a class, like so:

class GameState(object):
    def __init__(self):
        self.wins = 0
        self.losses = 0

state = GameState()

# later, in code:

state.wins += 1  # for a win

Global variables make a program messy and hard to figure out. You can improve the program by putting them together in a class. You can improve the program even more by making the connections explicit: add arguments to your functions where you pass in the class instance for the functions to modify, instead of having one instance at module level.

Other answers are suggesting just moving the variables into main(). That's a valid approach as well, especially for a small and simple program like this one. My preference for packing all the variables together into a class comes from my experience with small programs growing into larger programs over time. If you keep related things together it makes it easier to scale up a program. But for this program, just putting the variables into main() is fine.

Community
  • 1
  • 1
steveha
  • 74,789
  • 21
  • 92
  • 117
0

Your "guess, side, wins, losses" variables are globals, so you shouldn't be passing them into the "flipcoin" function. Doing this creates new local variables with the same name, and the global ones are never updated.

If you change these lines, it seems to work:

def flipcoin():
    global wins, losses
    side = coin[randint(0,1)]
    print 'You Chose: %s' %(guess)
    print 'Coin Is: %s' %(side)
    if guess == side:
        print 'you win'
        wins = wins + 1
    else:
        print 'you lose'
        losses = losses + 1

def main(token):
    while token > 0:
        getinfo(token)
        flipcoin()
Fred S
  • 985
  • 4
  • 11
0

As mentioned already, you should be returning in your functions. Limit the use of global variables.

"""This will be an attempt to create a simple 
coin toss game in which the player chooses heads or
tails and a random generator will pick one from a list
of either heads or tails and return whether or not 
the player chose correctly. I also hope to
create a scoreboard"""

from random import randint

coin = ['heads' , 'tails']

def getinfo(token):
    return raw_input('Heads or Tails?')

def flipcoin(guess, side, wins, losses):
    side = coin[randint(0,1)]
    print 'You Chose: %s' %(guess)
    print 'Coin Is: %s' %(side)
    if guess == side:
        print 'you win'
        wins = wins + 1
    else:
        print 'you lose'
        losses = losses + 1
    return side, wins, losses

def main():
    token = int(raw_input('how many games?'))
    wins = 0
    losses = 0
    side = coin[0]
    guess = 'heads'

    while token > 0:
        guess = getinfo(token)
        side, wins, losses = flipcoin(guess, side, wins, losses)
        if wins > losses:
            print "You are winning %s to %d" %(wins , losses)
        elif wins == losses:
            print "You are tied %s to %d" %(wins , losses)
        else:
            print "You are losing %s to %d" %(losses, wins)
        token -= 1
        print '%d games left' %(token)
        print 'wins: %s' %(wins)
        print 'losses: %s' %(losses)

if __name__ == '__main__':
    main()
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132