Craps Python Program

Write a python program to accomplish the following:. Play a dice game of Craps using a random number generator to simulate the roll of the dice; the code for the rolling of the dice should take place read more. The code is running a game of craps where on the first roll if I get a 7 or 11 I win, or if I get a 2,3,12 I lose. Browse other questions tagged python python-3.x.

  1. Python Craps Coding
  2. Blackjack Python Program

Last time I wrote about Python For Loops and If Statements. Today we will talk about how to combine them. In this article, I’ll show you – through a few practical examples – how to combine a for loop with another for loop and/or with an if statement!

Note: This is a hands-on tutorial. I highly recommend doing the coding part with me – and if you have time, solving the exercises at the end of the article! If you haven’t done so yet, please work through these articles first:

Note 2: On mobile the line breaks of the code snippets might look tricky. But if you copy-paste them into your Jupyter Notebook, you will see the actual line breaks much clearer!

For loop within a for loop – aka the nested for loop

The more complicated the data project you are working on, the higher the chance that you will bump into a situation where you have to use a nested for loop. This means that you will run an iteration, then another iteration inside that iteration.

Let’s say you have nine TV show titles put into three categories: comedies, cartoons, dramas. These are presented in a nested Python list (“lists in a list”):

You want to count the characters in all these titles and print the results one by one to your screen, in this format:

'The title [movie_title] is [X] characters long.'

How would you do that? Since you have three lists in your main list, to get the movie titles, you have to iterate through your my_movies list — and inside that list, through every sublist, too:

Note: remember len() is a Python function that results in an integer. To put this integer into a “printable” sentence, we have to turn it into a string first. I wrote about this in the previous Python For Loops tutorial.

I know, Python for loops can be difficult to understand for the first time… Nested for loops are even more difficult. If you have trouble understanding what exactly is happening above, get a pen and a paper and try to simulate the whole script as if you were the computer — go through your loop step by step and write down the results.

One more thing:
Syntax! The rules are the same ones you learned when we discussed simple for loops — the only thing that I’d like to emphasize, and that you should definitely watch out for, is the indentations. Using proper indentations is the only way how you can let Python know that in which for loop (the inner or the outer) you would like to apply your block of code. Just test out and try to find the differences between these three examples:

Example 2

Program

If statement within a for loop

Inside a for loop, you can use if statements as well.

Let me use one of the most well-known examples of the exercises that you might be given as the opening question in a junior data scientist job interview.

The task is:
Go through all the numbers up until 99. Print ‘fizz’ for every number that’s divisible by 3, print ‘buzz’ for every number divisible by 5, and print ‘fizzbuzz’ for every number divisible by 3 and by 5! If the number is not divisible either by 3 or 5, print a dash (‘-‘)!

Here’s the solution!

As you can see, an if statement within a for loop is perfect to evaluate a list of numbers in a range (or elements in a list) and put them into different buckets, tag them, or apply functions on them – or just simply print them.

Again: when you use an if statement within a for loop, be extremely careful with the indentations because if you misplace them, you can get errors or fake results!

Break

There is a special control flow tool in Python that comes in handy pretty often when using if statements within for loops. And this is the break statement.

Can you find the first 7-digit number that’s divisible by 137? (The first one and only the first one.)

Here’s one solution:

This loop takes every 137th number (for i in range(0, 10000000, 137)) and it checks during each iteration whether the number has 7 digits or not (if len(str(i)) 7). Once it gets to the the first 7-digit number, the if statement will be True and two things happen:

  1. print(i) –» The number is printed to the screen.
  2. break breaks out of the for loop, so we can make sure that the first 7-digit number was also the last 7-digit number that was printed on the screen.

Learn more about the break statement (and its twin brother: the continue statement) in the original Python3 documentation: here.

Note: you can solve this task more elegantly with a while loop. However, I haven’t written a while loop tutorial yet, which is why I went with the for loop + break solution!

Test Yourself!

It’s time to test whether you have managed to master the if statement, the for loops and the combination of these two! Let’s try to solve this small test assignment!

Create a Python script that finds out your age in a maximum of 8 tries! The script can ask you only one type of question: guessing your age! (e.g. “Are you 67 years old?”) And you can answer only one of these three options:

  • less
  • more
  • correct

Based on your answer the computer can come up with another guess until it finds out your exact age.

Note: to solve this task, you will have to learn a new function, too. That’s the input() function! More info: here.

Ready? 3. 2. 1. Go!

Solution

Here’s my code.

Note 1: One can solve the task with a while loop, too. Again: since I haven’t written about while loops yet, I’ll show you the for loop solution.
Note 2: If you have an alternative solution, please do not hesitate to share it with me and the other readers in the comment section below!

My logic goes:
STEP 1) I set a range between 0 and 100 and I assume that the age of the “player” will be between these two values.
down = 0
up = 100

STEP 2) The script always asks the middle value of this range (for the first try it’s 50):

STEP 3) Once we have the “player’s” answer, there are four possible scenarios:

    • If the guessed age is correct, then the script ends and it returns some answer.
    • If the answer is “less”, then we start the iteration over – but before that we set the maximum value of the age-range to the guessed age. (So in the second iteration the script will guess the middle value of 0 and 50.)
    • We do the same for the “more” answer – except that in this case we change the minimum (and not the the maximum) value:
    • And eventually we handle the wrong answers and the typos:

Did you find a better solution?
Share it with me in the comment section below!

Conclusion

Now you’ve got the idea of:

  • Python nested for loops and
  • for loops and if statements combined.

They are not necessarily considered to be Python basics; this is more like a transition to the intermediate level. Using them requires a solid understanding of Python3’s logic – and a lot of practicing, too.

There are only two episodes left from the Python for Data Science Basics tutorial series! Keep it going and continue with the Python syntax essentials!

  • If you want to learn more about how to become a data scientist, take my 50-minute video course: How to Become a Data Scientist. (It’s free!)
  • Also check out my 6-week online course: The Junior Data Scientist’s First Month video course.

Cheers,
Tomi

ewoks4life
Hi there,
I had an idea for a new Craps prop bet and was trying to write some Python code that would simulate the bet, but ran into an issue.
My assumption was that if you flipped the rules of a bet and you flipped the payouts of the bet the house edge on that new hypothetical bet could be expressed by simply changing the sign of the house edge on the original bet.
Say you turned the hard 8 bet into a lay the hard 8 bet. You win on 7 or easy 8 before the next hard 8. You’re essentially acting like the casino at that point and the payouts go from 9:1 to 1:9. Shouldn’t the house edge on this new hypothetical bet be -9.09% since the house edge on hard 8 is 9.09%?
To test my assumption, I ran a 10 million round simulation of the traditional hard 8 bet and laying the hard 8 and was surprised to find that while the original hardway bet did show a house edge of ~ 9%, laying the hard 8 showed a house edge of ~ -1%.
I’m trying to determine if the mistake I’ve made is in the assumption that a house edge flips if you flip the payouts and the rules of the bet or if the mistake I’ve made is in the Python code I’ve written.
Any thoughts?

Returns:
Hard eight player edge: -9.11%
Lay hard eight player edge: 0.98%
Program
ThatDonGuy
Your code appears correct.
Your mistake is, while you are 'acting like the casino' when you lay the bet, the casino is betting 1/9 against your 1, rather than 1 against your 9, but your house edge is based on your bet rather than the casino's.
ewoks4life
I'm sorry but I really don't understand what you mean. Are you saying that this bet really would only be an advantage of 1% if the player could make it? I don't see how that could be.
If the casino has a 9% advantage on a hard 8, surely the player would have a 9% advantage if they could book the same exact bet as the casino, right? Putting my $9 up against the casino's $1 (which is the same scenario that plays out on a standard hard 8 bet, just reversed) is the same as putting my $1 up against the casino's $(1/9).
odiousgambit
fair odds are 10:1 ... I'd guess if they ever let you play darkside you'd put up something like $15 to win $1
if you only had to put up $9 I agree that the player would get the casino's exact edge for himself
in any case that has to be the most unheard of bet in a casino - to take the large end of the bet. I can't think of anything like that for the moment
I take it back, the most unheard of thing is to intentionally allow a bet with a player edge
the next time Dame Fortune toys with your heart, your soul and your wallet, raise your glass and praise her thus: “Thanks for nothing, you cold-hearted, evil, damnable, nefarious, low-life, malicious monster from Hell!” She is, after all, stone deaf. ... Arnold Snyder
RS
Thanks for this post from:

I'm sorry but I really don't understand what you mean. Are you saying that this bet really would only be an advantage of 1% if the player could make it? I don't see how that could be.
If the casino has a 9% advantage on a hard 8, surely the player would have a 9% advantage if they could book the same exact bet as the casino, right? Putting my $9 up against the casino's $1 (which is the same scenario that plays out on a standard hard 8 bet, just reversed) is the same as putting my $1 up against the casino's $(1/9).


'House Edge' is actually a bit of a misnomer.
The casino isn't betting $1 against your $1. The casino is betting $9 against your $1. You just have a 9% disadvantage.
The casino has a 1% or so advantage on their $9 bet. You have a 9% (or so) disadvantage on your $1 bet.
IE: Player $1 bet: has 10 ways to lose $1 and 1 way to win $9. The total money wagered is $11. The sum of outcomes (10*-1 + 1*9) is -1.

Python Craps Coding


-1/11 ~ -9%ProgramBlackjack python program
The casino, on the other hand, is betting $9. 1 way to lose $9 and 10 ways to win $1. Total money wagered is $99. The sum of outcomes (-9*1 + 1*10) is +1.
+1/99 ~ +1%.ThatDonGuy
Thanks for this post from:

'House Edge' is actually a bit of a misnomer.
The casino isn't betting $1 against your $1. The casino is betting $9 against your $1. You just have a 9% disadvantage.
The casino has a 1% or so advantage on their $9 bet. You have a 9% (or so) disadvantage on your $1 bet.


This. +1. Whatever the term for 'exactly' is this week.
The amount of the house edge is the same on both sides of the place bet because you are putting up 9x what the casino is.
The percentage, on the other hand, is different because each side is betting a different amount.
ewoks4life
Thank you both for the explanation. It's much clearer now. I looked over the code 10x and knew it was making the bet and payouts in the exact opposite way so figured it must have just been my lack of understanding the math.

in any case that has to be the most unheard of bet in a casino - to take the large end of the bet. I can't think of anything like that for the moment
I take it back, the most unheard of thing is to intentionally allow a bet with a player edge


When it comes table games I think Craps has the largest lay bet available at 19 to 41 when laying the 4 or 10 (when factoring in the vig). I can't think of anything with a smaller payout compared to the original bet.

Blackjack Python Program


You can lay much higher odds on sports. The infamous No Safety SB bet comes to mind.
This is just a thought experiment for me, not a serious idea that I'm going to try to sell to casinos or anything. They wouldn't want this bet. Obviously if a casino did implement this it would not be at 1 to 9. It would be at 1 to 10 or 1 to 11. Much like how 99% of sports betters don't lay -900 on No Safety in the SB, 99% of players would never lay 1 to 9 on a bet at a Craps table even if they had an advantage so no players would ever lay 1 to 10 or 1 to 11 on a bet where the casino had an advantage so it would just never be implemented.
I'm just messing around trying to intersect my interest in (basic) programming with my interest in gambling.
Comments are closed.