Craps Python Code

Posted on by admin
Craps Python Code 3,9/5 1874 reviews

If you think coding a dice in Python is hard you are complete wrong! You don’t need 800 lines to do it, 5 is more than enough.

TAKE A LOOK AT THE MORE COMPLETE VERSION OF THIS POST OVER HERE: Handle your 1st Python project and make a Dice Simulator Web App

Craps

Time again for a game script. How it works This is a classic “roll the dice” program. We will be using the random module for this,since we want to randomize the numberswe get from the dice. We set two variables (min and max), lowest and highest number of the dice. We then use a. After removing the recursion there are still a couple of ways I'd change the code: You can check if an item is in a list, rather than using. 'a' in 'a', 'b'. The code should default empty input to y if when using Y/n. To do this compare to n rather than y. I'd move the prints to the main. I'd move the rolling line in roll, to it's own.

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. Any other number I keep rolling till I either get what I rolled the first time or I roll a 7. Browse other questions tagged python python-3.x or ask your own question. The Overflow Blog Podcast 291: Why developers are demanding. At the bottom of this description you can find our code. Be sure to like, share and comment to show your support for our tutorials. Craps in Python Part 2: Program - Duration: 21:52.

Welcome everybody! Today we will make a simple dice simulator from scratch. If you are just starting to code, this tutorial is for you.

If you need a simpler tutorial I suggest you to take a look at this tutorial: hello world! in Python

Lets start by opening Python. Go to Windows main menu and select IDLE.

Now, lets click File >> New file. Here is where we will write our code.

First we import the library that allows us to choose random numbers.

Now, we generate a random number and save it in a variable. We will call it selected.
This library has a function called randint(). The randint(min number, max number) requires 2 parameters (the lowest number and the highest number between we will pick our number randomly). In this case, our dice goes between 1-6.

If we want to show our selected number, we must use print(). Your code should look like this:

If we press F5, a message will ask us to save the code and then it will start running. If everything went as expected, we should see something like this:

In my case, the random chosen number was 5. If we close the console and run the code again pressing F5, the chosen number will be different.

Nice, we already have our main engine working, now it’s time to make it look more appealing. To do that we will add some improvements:

If we run the code again, we should see a little message and the random number. Congrats! but we can improve it even more. Our code runs only once and then it close. What we need is to keep it running. To accomplish this, we will used while.

If you run this code, you will see that the dice will keep rolling as long as you press any key.
We could keep improving the code even more, but lets leave it here until another day. This example is great to start coding in python. Hope you liked it and see you soon!

TAKE A LOOK AT THE MORE COMPLETE VERSION OF THIS POST OVER HERE: Handle your 1st Python project and make a Dice Simulator Web App

If you have any trouble, leave me a comment.

Planning Strategies¶

After each round, the program should ask the user if he/she wants to continue playing. The game continues until the player runs out of money, or the player chooses to exit.

Similar to Rock Paper Scissors (as well as any multi-step, complex program), planning out your code will be highly beneficial in writing cleaner and more organized solutions. However, this time, you will be coming up with your own functions. Please remember to heed the following guidelines:
  • The majority of your program should be functions. Break the game down into simple steps or instructions - each of those will have its own function.
  • Each function should have a single purpose. If you find that you have written a function that is doing multiple things, it means you should break it down further into more functions. It’s okay to refactor code mid-development!
  • The one exception to the above should be your main game function, which will contain the game loop, as well as any persistent variables. Try to keep this one as small as possible, utilizing functions as much as you can.
  • Take advantage of return statements. Your code should not have any global variables. Data should be passed to functions through arguments, and from functions through the return statement.
  • Which variables will you have to keep track of during all the rounds, and after all the rounds are completed? Make a note of which ones should be initialized outside of your loop.
  • Be sure to provide the player with the appropriate prompts, letting them know their options and limitations for input.
  • Consider what control structures are necessary for the program.
  • Consider the order of occurrences - what should the program do first?
Blackjack python code example

However, unlike Rock Paper Scissors, in this lab you will be required to validate user’s inputs. This means that:

  • Players should not be able to enter a negative number as a bet
  • Players should not be able to bet more money than they have in their bank
  • Players should not be able to bet decimal amounts of money - only whole numbers. (recall: A boolean to check if x is an integer or not would be: xint(x). This will return True is x is an integer and False otherwise. It works because of math.)
  • When prompted with the choice of ending the game or continuing, the question should repeat until the user has entered one choice or the other.

This also means that your labs will have quite a few while loops. Keep that in mind!

A good place to start would be to write the functions that you plan on writing. You do not need to write the code itself, only the purpose of each function. Here are a few examples:
  • function name: roll2dice
    • arguments: none
    • purpose: generates a random dice roll for two dice and prints out that the two rolls are
    • returns: the sum of the dice roll
  • function name: get_bet
    • arguments: bank amount
    • purpose: to get the amount to be bet from the user. Bet must be a positive integer and no more than they have in their bank. Should repeatedly ask the user to make a bet until they enter a valid one
    • returns: the chosen valid bet amount

And so on. The above are examples, and do not have to be followed. There are many ways these games can be organized. However, notice that the names of my functions are verbs - just remember that functions should do things, and therefore have names that reflect what they do.

Craps Game Python Code

You should name your file FILN_craps.py, where FILN is your first initial and last name, no space.