Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, 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:

Python
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.
Posted

The following statements just calculate the values
Python
bankrol1 + 1
bankroll2 -1

but that is all, they do not save them. They should be
Python
bankroll += 1    # add 1 to bankrol1
bankroll2 -= 1   # subtract 1 from bankroll2 

You also need to add two statements for the case when player2 wins. I would guess something like:
Python
bankrol1 -= 1    # subtract 1 from bankrol1
bankroll2 += 1   # add 1 to bankroll2 
 
Share this answer
 
To extend on the previous answer, this comes down to mutability.
Basically, integers are immutable. When you add two integer it returns a brand new integer. In this case, you have a variable bankroll which is assigned to be the original integer. When you add 1 to this variable, the operation returns a new integer. You need to overwrite the original variable with the updated one.

Python
# Assign an integer to your variable
bankroll = 10

# We'll separate this out for ease of viewing.
# basically bankroll + 1 returns the result of the addition operation
# We'll store it to a temp variable for now
temp = bankroll + 1

# Overwrite the original bankroll variable with the value of the
# temp variable
bankroll = temp


Now we could do this in one line as follows
Python
bankroll = bankroll + 1


This does the same thing without using a temporary variable. However, python has some nice syntactic sugar for doing this sort of operation
Python
bankroll += 1


In fact this are available for many of the basic operations
Python
bankroll = 10
print(bankroll) # prints 10

bankroll += 1
print(bankroll) # prints 11

bankroll -= 1
print(bankroll) # prints 10

bankroll *= 2
print(bankroll) # prints 20

bankroll /= 2
print(bankroll) # prints 10
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900