last day (17 days later) » 

10:26
1
A: Craps Simulator Exercise

GraipherFirst, Python has an official style-guide, PEP8, which programmers are encouraged to follow. This makes it easier for other Python programmers to read your code. PEP8 recommends using lower_case both for variable and function names, which I have used in the code below. In your output function yo...

@Meph-: The first one is a tuple and the latter one a set. For small collections there will be basically no difference, but a set has O(1) membership testing, while a tuple is slightly faster to construct, but has O(n) membership testing. On my machine 1 in (2, 3, 12) takes 53.9 ns ± 0.361 ns and 1 in {2, 3, 12} takes 55.2 ns ± 2.07 ns.
@Meph-: And for completeness sake (and to show that it is better than chaining or), 1 == 2 or 1 == 3 or 1 == 12 takes 73.3 ns ± 0.658 ns.
I'll put the formatting changes to use! Thanks. As to using "in", i simply havent learned that yet(tuples, sets),it will probably come later in my programming book, but I'll implement it right away. Really seems to be the better choice. Sorry for the die/dice mixup haha.
Okay, it seems like print(f"The player won {wins} of {total} games ({wins/total:0.2%})") doesn't work. i think you can't calculate anything in there
@Kevin: No, it should work. You can use arbitrary Python expressions inside f-strings (and even some subset of Python expressions like attribute and item access in the format string you are using).
@Graipher it recognizes the : in {wins/total:0.02} as the end of the whole statement in the brackets, not sure why. Everything after and including the : is seen as string
@Kevin: Well, I just copy & pasted it (from your comment), just to be sure, with wins, total = 10, 23 and it works on my machine.
@Kevin: Maybe you are missing the %? You either need that or a :0.2f.
10:26
@Graipher Here's a screenshot: imgur.com/a/nMUsSFY
@Kevin: Looks fine to me? It even correctly colors the expression part. Have you tried running that line?
Hey!
So as commented, It prints The player won 100 of 200 games (0.5). What throws me off is that the last } isn't in red, but in green, indicating it's part of the string.
I changed it to 0.2% btw
If I do (Python 3.6.3) it:
>>> print(f"The player won {wins} of {total} games ({wins/total:0.2%})")
The player won 10 of 23 games (43.48%)
The wrong coloring of the closing } could just be a problem with your syntax highlighter, but getting the wrong output means you are doing something else wrong...
yeah, in an interactive session it does work, but not in the full module.. weird, I'm just trying to find the error right now
I Moved the print to main(), still the same issue
10:44
Very weird...
`#craps.py -- Simulates multiple games of craps
# and estimates the probability that the player wins.

import random

def rolldice():
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll = dice1 + dice2
return roll

def sim_one_game():
initial_roll = rolldice()
if initial_roll == 2 or initial_roll == 3 or initial_roll == 12:
return True # won
elif initial_roll == 7 or initial_roll == 11:
return False #lost
else:
#Roll until roll is 7 or initial roll
roll = rolldice()
while roll != 7 and roll != initial_roll:
if you paste the whole code into your editor, is the print highlighted correctly?

I mean as it gives a wrong output it can't be my editor(Atom)
ugh not formatted right
there you go

  last day (17 days later) »