1
#Setting up RNG
loop = "y"
while loop == "y" or loop == "yes":
    from random import randint
    dice = (randint(1,10))
    dice2 = (randint(1,10))
    roll = (dice + dice2)
    win = 3
    loss = 2
    cash = 20
    if roll == 3 or roll == 7 or roll == 11 or roll == 17:
        cash += (win)
    else:
        cash -= (loss)
    #Starting game
    print("""Welcome to, Gambling for School!

    You have $20 and must earn as much money as possible

    If you roll a 3, 7, 11, or 17, you will win $3 but any other number 
takes $2

    You have a 20% of winning
""")
    x = input("Press ENTER to start.")
    #Results
    if roll == 11 or roll == 8 or roll == 18:
        print("You rolled an " + str(roll) + "!")
    else:
        print("You rolled a " + str(roll) + "!")
    print("")
    print("Cash - $" + str(cash))
    loop = input("Continue? (Y/N) ").lower()

Had to change the indenting to show it as code

When it runs, I hit enter to start the game and it adds and subtracts correctly but when I select continue, it plays as if I never lost or gained any money. It is 1AM and idk if my brain died but I can't think of anything to fix it

kalehmann
  • 4,821
  • 6
  • 26
  • 36
VossZero
  • 11
  • 1

2 Answers2

0

You reinitialize the variable cash with 20 before every game. To fix the game, just move that code out of the loop.

The initialization of win and loss can also be moved out of the loop since they do not change.

Same for the from random import randint statement, it is considered a good practice to put all import statements at the top of the file.

from random import randint

#Setting up RNG
loop = "y"
win = 3
loss = 2
cash = 20
while loop == "y" or loop == "yes":
    dice = (randint(1,10))
    dice2 = (randint(1,10))
    roll = (dice + dice2)

    if roll == 3 or roll == 7 or roll == 11 or roll == 17:
        cash += win
    else:
        cash -= loss
    #Starting game
    print("""Welcome to, Gambling for School!

You have $20 and must earn as much money as possible

If you roll a 3, 7, 11, or 17, you will win $3 but any other number 
takes $2

You have a 20% of winning
""")
    x = input("Press ENTER to start.")
    #Results
    if roll == 11 or roll == 8 or roll == 18:
        print("You rolled an " + str(roll) + "!")
    else:
        print("You rolled a " + str(roll) + "!")
    print("")
    print("Cash - $" + str(cash))
    loop = input("Continue? (Y/N) ").lower()
kalehmann
  • 4,821
  • 6
  • 26
  • 36
0

I would reorder your code to make the control-flow more clear.

For comparison "if a in several elemets" you should use set()s - they are very efficient when looking up if something is in them (and for other set-operations).

For printing look up the str.format() or python 3.6+ string interpolation: PEP-498

You only ever use the sum of 2 random numbers, you can get them in one go using random.choices(iterable, k=2)

from random import choices

cash = 20
winAmount = 3
lossAmount = 2

#Starting game
print("""Welcome to, Gambling for School!

    You have $20 and must earn as much money as possible

    If you roll a 3, 7, 11, or 17, you will win $3, else you loose $2

    You have a 20% chance of winning
""")
x = input("Press ENTER to start.")

lucky_numbers = {3,7,11,17}

# needed for outputting text
pluralize = {8,11,18}
win = False


loop = "y"
while loop and loop[0]== "y":
    sum_dice = sum(choices(range(1,11), k=2))

    if sum_dice in lucky_numbers:
        win = True
        cash += winAmount
    else:
        win = False
        cash -= lossAmount

    print("You {}. You rolled a{} {}!".format(
        "won" if win else "lost",
        "n" if sum_dice in pluralize else "", 
        sum_dice))

    print("")
    print("Cash - $" + str(cash))
    loop = input("Continue? (Y/N) ").lower().strip()

Output:

Welcome to, Gambling for School!

    You have $20 and must earn as much money as possible

    If you roll a 3, 7, 11, or 17, you will win $3, else you loose $2

    You have a 20% of winning

Press ENTER to start.
You lost. You rolled a 6!

Cash - $18
Continue? (Y/N) y
You lost. You rolled a 16!

Cash - $16
Continue? (Y/N) y
You lost. You rolled a 16!

Cash - $14
Continue? (Y/N) y
You lost. You rolled a 15!

Cash - $12
Continue? (Y/N) y
You won. You rolled a 7!

Cash - $15
Continue? (Y/N) n

Beside print formatting the outputs use a ternary (do x if this else y) operator. More here: Does Python have a ternary conditional operator?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69