Reputation: 3
i just started coding in Python. I have no prior experience in any other programming language so i hope this forum will be a great place for me to learn about coding! so here it is:
import random
bankroll = 10
bankroll2 = 10
def player():
player1 = random.randrange(0, 5)
player2 = random.randrange(0, 5)
if player1 > player2:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "Player 1 Win"
bankroll + 1
bankroll2 -1
print bankroll
print bankroll2
elif player1 < player2:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "Player 2 Win"
print bankroll
print bankroll2
else:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "It's a tie!"
print bankroll
print bankroll2
print ""
player()
player()
player()
player()
player()
player()
player()
right now the bankrolls are always restarting to 10, i understand why but i don't know how to avoid it. My goal is to keep track of the bankroll.
If anyone could point me in the right direction it would be greatly appreciated.
Upvotes: 0
Views: 104
Reputation: 55448
You're not changing bankroll
anywhere, bankroll + 1
adds one to bankroll
then returns the value, but doesn't change bankroll
Try bankroll = bankroll + 1
, or the shorter form bankroll += 1
Same with bankroll2
, try bankroll2 += -1
And of course if you insist on using the global variables in the module:
def player():
global bankroll
global bankroll2
# This will allow your function to access and change the bankroll vars you defined
Maybe for your next project you can try OOP to avoid using globals as your code will quickly get messy, try this Avoid using global without confusing new programming students in Python?
Using a class Player
seems handy here since you'll need to instantiate player1, player2 etc
Upvotes: 4
Reputation: 336418
bankroll + 1
only calculates, but doesn't do anything with the result. You need to assign that back to a variable, for example bankroll = bankroll + 1
, or (more concisely) bankroll += 1
.
However, this leads to a new problem: player
is a function with its own local variable scope. Therefore, you should pass the starting bankrolls to it and return the results from it:
def player(br1, br2):
player1 = random.randrange(0, 5)
player2 = random.randrange(0, 5)
if player1 > player2:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "Player 1 Win"
br += 1
br2 -= 1
print br
print br2
# etc.
return br, br2
Then call it like bankroll, bankroll2 = player(bankroll, bankroll2)
.
Upvotes: 1
Reputation: 17434
In order to set bankroll
to a different value, use the assignment operator (=
), e.g. like bankroll = bankroll + 5
. There are also augmented assignment operators that work similarly, like bankroll += 5
.
Now, assigning to a variable in a function makes it a local variable, the global one is then ignored in that function. So you also have to tell Python that it's a global one and that you don't need a local one using global bankroll
inside that function.
Upvotes: 2