Craps In Python
I don't know how to play craps but i've looked up the rules and the first one says that if the come out roll is 2,3, or 12 the shooter craps out and they lose their money. I'm not sure how to implement this in code though. Also the way it is explained is a bit convoluted. If anyone knows what's supposed to happen I'd appreciate the help. Simple Python craps game. Programming Forum. Software Development Forum. Discussion / Question. Melly3 6 Newbie Poster.
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.
- 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 thereturn
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?
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 returnTrue
isx
is an integer andFalse
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!
- 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.
You should name your file FILN_craps.py
, where FILN is your first initial and last name, no space.
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
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.
Craps Python Github
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:
Craps In Python
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
Craps Game In Python
If you have any trouble, leave me a comment.