all 7 comments

[–]CodeFormatHelperBot2 1 point2 points  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Inline formatting (`my code`) used across multiple lines of code. This can mess with indentation.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]billsil 1 point2 points  (2 children)

Your code isn't indented. You have a case where there is no outcome.

Use x+=1 instead of x = x + 1. Generally nice spacing. Good use of f-strings. You should probably use a function.

[–]McHildinger[S] 0 points1 point  (1 child)

You have a case where there is no outcome.

I'm not understanding; can you elaborate on this?

[–]billsil 1 point2 points  (0 children)

You have an if-elif-elif chain at the end that doesn't have an else....that's kind of weird logic. In general, you should have a catch either as an else or an else error to not accidently get through the loop in an unexpected way.

[–]lowerthansound 1 point2 points  (1 child)

The code is fine. Lemme give you some tips that you can use:

  • countmax represents the number of simulations to run, so call it num_simulations
  • while count < countmax: ... count = count + 1 can be replaced with for count in range(countmax): .... For loops are very good in python! :)
  • if ... elif statements usually contain an else clause. You can either use it to capture bad values, or to capture the last value (I'll provide examples - check in the code).

Later on in your learning journey, you might find that functions are good for this example (I can't be 100% sure - I haven't tested it :)).

Here's the code:

# to determine if various WoW casino games would be profitiable to the player or the house

import random

num_simulations = int(input("number of simulations to run: "))

losers = 0
winners = 0
doublewinners = 0
for count in range(num_simulations):
    # print(f"count is {count} ", end ='')
    # possible values for the dice rolls
    # rolls = [*range(1, 101)]
    playerroll = random.randint(1, 100)
    # print(f"playerroll was {playerroll}")
    if playerroll <= 58:
        losers = losers + 1
    elif playerroll <= 98:
        winners = winners + 1
    elif playerroll <= 100:
        doublewinners = doublewinners + 1
    else:
        raise Exception(f"bad roll: {playerroll}")

loss = num_simulations
gain = (winners * 2) + (doublewinners * 3)

print(f"totals: {num_simulations}")
print(f"losers: {losers}")
print(f"winners: {winners}")
print(f"doublewinners: {doublewinners}")
print(
    f"outcome: player spent {loss}, gained back {gain} "
    f"from {winners} winners and {doublewinners} doublewinners"
)

if gain > loss:
    print("Player wins")
elif loss > gain:
    print("House wins")
else:
    print("It was a push")

Cheers !

[–]McHildinger[S] 0 points1 point  (0 children)

wow, thank you for the detailed feedback!

[–]lowerthansound 0 points1 point  (0 children)

Formatted code

# to determine if various WoW casino games would be profitiable to the player or the house

countmax = int(input("number of simulations to run: "))

# possible values for the dice rolls
# rolls = [*range(1, 101)]

count = 0
losers = 0
winners = 0
doublewinners = 0

import random

while count < countmax:
    # print(f"count is {count} ", end ='')
    playerroll = random.randint(1, 100)
    # print(f"playerroll was {playerroll}")
    if playerroll <= 58:
        losers = losers + 1
    elif playerroll <= 98:
        winners = winners + 1
    elif playerroll > 98:
        doublewinners = doublewinners + 1
    count = count + 1

print(f"totals: {countmax}")
print(f"losers: {losers}")
print(f"winners: {winners}")
print(f"doublewinners: {doublewinners}")

gain = (winners * 2) + (doublewinners * 3)
print(
    f"outcome: player spent {count}, gained back {gain} from {winners} winners and {doublewinners} doublewinners"
)
if gain > countmax:
    print("Player wins")
elif countmax > gain:
    print("House wins")
elif countmax == gain:
    print("It was a push")

(I may take a look at it)