Question:
ANYONE KNOW HOW TO DO THIS?
anonymous
2017-10-02 00:26:16 UTC
Write a program which will simulate a 3-dice game using random numbers.
The game has 6 dice. Each die has numbers from 1 to 12 on it.
Each player (Bob and Tom) chooses 3 dice from the pile and rolls them. The sum of each player's 3 rolls is found.
If Bob's sum is higher than Tom's sum, then Bob wins the round. The same goes for Tom. If the sums are the same, there is a tie.
You do NOT need any kind of new data structure - no lists are needed. Just use 6 variables for the rolls, 3 for Tom and 3 for Bob.
The program displays all the rolls and the result of the playing against each other.
The program plays the game for the number of times specified by the user. It keeps track of how many rounds Bob won, how many Tom won and how many ties there were.
After all the rounds have been played, the results are shown.
Write the design for the program first - that will give you your comments!
Sample run:

How many rounds to play? 5
Bob's rolls: 10 2 2
Tom's rolls: 10 12 10
Tom won!
Bob's rolls: 7 2 4
Tom's rolls: 3 4 12
Tom won!
Bob's rolls: 7 1 11
Tom's rolls: 2 3 1
Bob won!
Bob's rolls: 12 11 3
Tom's rolls: 3 11 12
It's a tie!
Bob's rolls: 5 8 11
Tom's rolls: 1 6 11
Bob won!
Results:
5 Rounds played
Bob: 2 Tom: 2 Ties: 1
Twelve answers:
Rahat
2017-10-04 20:13:18 UTC
Get random number support by adding:



import random # at the top of your source file



Then you can roll a single die with:



def roll1() :

.... return random.randint(1,12)



With that done, just call roll1() whenever you want a new random roll.
Tor-Bjorn
2017-10-03 23:59:20 UTC
I'd be able to do this in several programming languages, but not in Python. So when you do your homework, why not post back here and show us how you did it! Come on. After all, you're the one who took the class!
riya
2017-10-03 11:01:22 UTC
no
PoohBearPenguin
2017-10-03 00:53:54 UTC
Instead of jumping straight into programming, pretend you're going to play this game in real life and consider the instructions.



Ok, now turn each instruction from the real world into a python statement.



As you start writing statements you'll figure out what variables you'll need, and if you find yourself repeating chunks of code over and over you may want to consider turning those into a function.



There you go, you should now have a mostly working program. Check to make sure you've got all the details and that should be it.
James
2017-10-02 21:51:23 UTC
If I can learn to do it......you can learn to do it!
ProDev
2017-10-02 16:52:01 UTC
auh
Anand
2017-10-02 12:21:10 UTC
Yes they know it better. Because they have their skills and experiences.
husoski
2017-10-02 01:10:23 UTC
Get random number support by adding:



import random # at the top of your source file



Then you can roll a single die with:



def roll1() :

.... return random.randint(1,12)



With that done, just call roll1() whenever you want a new random roll.



To get three of them in a single variable:



tom = roll(), roll(), roll()



To add those up, Python has a sum() function built in:



tom_score = sum(tom)



That's small enough that you can type stuff in interactive mode (or the Idle shell) and experiment until you figure out what will work best in your simulation.
wyatt skaggs
2017-10-02 00:51:22 UTC
python 3
?
2017-10-02 00:42:07 UTC
Not specifically this problem, but I could figure it out with some time. You'll need 6 variables to store the rolls, as it says, as well as a random number generator, a variable for total rounds, and variables for any 2 of: Bob's wins, Tom's wins, and ties. You could do all three, but the third would be redundant since you can find it from the total rounds and the other two.



Once you have all these things, and decide where in the structure you want to initiate them, you'll want to make a for loop to generate the dice rolls and compare the totals for however many rounds the user wants, and use if/else structures to choose the appropriate results. Then you can close the for loop and display the final results.
?
2017-10-03 08:42:23 UTC
It sounds like a program I could have written 25 years ago in College using BASIC and a string of "IF-THEN" commands. Try that.
?
2017-10-02 14:48:45 UTC
no nothing


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...