Search code examples
pythonrandomdice

Dice game add a payment method


I have this code and I want to add a payment method to make the person start with 6$. Each time the dice roll if they are equal you add the sum of the dice to the guy's money; if they are not equal you subtract 2.

import random
a = random.randrange(1 , 6)
b = random.randrange(1 , 6)
print (a , b)
if a == b:
    print("you win" , 2 + a + b)
else:
    print("you lose")

Trial code that almost worked but is highly stupid:

import random
a = random.randint(1 , 2)
b = random.randint(1 , 2)
print (a , b)
e = 1
g = 1
for x in range(g):
    e = 6
    g = e/2
    if a == b:
        print("you win")
        e = a + b + e 
        
    else:
        print("you lose" )
        e = e - 2
    print(e)

Solution

  • You first declare the money in the account. You do a loop for 10 trials, you can change the loop for whatever you like (while True: or something similar).

    For every loop, you give your two dices a random number. Then, you check if the numbers are equals if diceA == diceB:. If this condition is True, you add to that person money the sum of dice A and dice B. If the condition is not True, then the program to the else: statement. We remove 2 from the personMoney.

    import random
    
    personMoney = 6
    
    for i in range(10):
        diceA = random.randint(1, 6)
        diceB = random.randint(1, 6)
        if diceA == diceB:
            personMoney += diceA + diceB
        else:
            personMoney -= 2
        print(personMoney)