Jump to content

Python dice roll help

Go to solution Solved by dom1310df,

Hello all, I am supposed to create a program in Python that gives the output of the something like this

Welcome to the dice roll simulator.

How many sides does the dice have? 4

How many times would you like to roll the dice? 3

** , 2 dots.

**** , 4 dots.

* , 1 dot.

Total value of all rolls:  7

 

I have been working on figuring out this code for a while.  The only guideline I'm supposed

to follow is using diceRoll = random.randint(1, diceSides) to get the final output.  However,

I can't even think of a place to start.  could anyone give me some pointers on what to do?

Any help is appreciated

 

-CJ

 

Edit: I am also supposed to do this using a while loop

OK, here's a better version, with comments explaining what's going on. Hope this helps :)

 

http://pastebin.com/GGmUBvbC

 

EDIT: You'll need to implement a way to catch the error thrown up if a user doesn't enter a number for either question, then ask the question again. You could even try and interpret when numbers are entered as words. Currently, int(raw_input("")) throws up an error for anything but a number.

 

EDIT 2: You didn't say whether you are using Python 2 or Python 3. My code is for Python 2. For Python 3, replace raw_input with input.

Hello all, I am supposed to create a program in Python that gives the output of the something like this

Welcome to the dice roll simulator.

How many sides does the dice have? 4

How many times would you like to roll the dice? 3

** , 2 dots.

**** , 4 dots.

* , 1 dot.

Total value of all rolls:  7

 

I have been working on figuring out this code for a while.  The only guideline I'm supposed

to follow is using diceRoll = random.randint(1, diceSides) to get the final output.  However,

I can't even think of a place to start.  could anyone give me some pointers on what to do?

Any help is appreciated

 

-CJ

 

Edit: I am also supposed to do this using a while loop

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/
Share on other sites

Link to post
Share on other sites

Hello all, I am supposed to create a program in Python that gives the output of the something like this

Welcome to the dice roll simulator.

How many sides does the dice have? 4

How many times would you like to roll the dice? 3

** , 2 dots.

**** , 4 dots.

* , 1 dot.

Total value of all rolls:  7

 

I have been working on figuring out this code for a while.  The only guideline I'm supposed

to follow is using diceRoll = random.randint(1, diceSides) to get the final output.  However,

I can't even think of a place to start.  could anyone give me some pointers on what to do?

Any help is appreciated

 

-CJ

 

Edit: I am also supposed to do this using a while loop

I'm currently studying python so I'm not that familiar with syntax. I'll try to suggest some logic(? algorithm?).

Let:

sides = number of sides

roll = number of rolls

total = total value of rolls

counter = rolls counter

diceRoll = temporary variable for values

str = string to print

 

//start the loop

While (counter <= roll){

  //roll the dice

  diceRoll = random.randint(1, diceSides);

 

  //loop to print

  for(x=1;x<=diceRoll;x++){

   //check the number of loop to see the number of * (I hope you get it)

    if(x==diceRoll){

      str = str + ", " + diceroll + " dots."

    }else{

      str = str + "*"

    }

  }

//print the **, 2 dots.

print str;

total = total + diceRoll;

}

print total;

 

 

I might have combined a few programming languages or something, but I hope you get what I'm trying to say. I've not tested that so just adjust it and test :D

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6305934
Share on other sites

Link to post
Share on other sites

I'm currently studying python so I'm not that familiar with syntax. I'll try to suggest some logic(? algorithm?).

Let:

sides = number of sides

roll = number of rolls

total = total value of rolls

counter = rolls counter

diceRoll = temporary variable for values

str = string to print

 

//start the loop

While (counter <= roll){

  //roll the dice

  diceRoll = random.randint(1, diceSides);

 

  //loop to print

  for(x=1;x<=diceRoll;x++){

   //check the number of loop to see the number of * (I hope you get it)

    if(x==diceRoll){

      str = str + ", " + diceroll + " dots."

    }else{

      str = str + "*"

    }

  }

//print the **, 2 dots.

print str;

total = total + diceRoll;

}

print total;

 

 

I might have combined a few programming languages or something, but I hope you get what I'm trying to say. I've not tested that so just adjust it and test :D

Thank you for your suggestions, my main question is how do I stop the loop at a certain number of rolls. for example if the person inputs that they want to roll it 5 times how do I make the program roll it the number they input

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6306028
Share on other sites

Link to post
Share on other sites

Thank you for your suggestions, my main question is how do I stop the loop at a certain number of rolls. for example if the person inputs that they want to roll it 5 times how do I make the program roll it the number they input

 

 

Assign the input to the variable roll.

 

count the number of loops.

modify this line (I'm not that sure about this) to limit the number of loops to the value of roll.

While (counter <= roll){

 

A little disclaimer:

I am not that knowledgeable with python. This code is not tested with any language I'm using. Please modify this to fit to the syntax and rules of python and everything. 

But I'm pretty sure about the logic and flow of this process, just need some tweaks and optimization.

I hope (wish, pray) I'm guiding you to the right path :D :D :D

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6306124
Share on other sites

Link to post
Share on other sites

Assign the input to the variable roll.

 

count the number of loops.

modify this line (I'm not that sure about this) to limit the number of loops to the value of roll.

While (counter <= roll){

 

A little disclaimer:

I am not that knowledgeable with python. This code is not tested with any language I'm using. Please modify this to fit to the syntax and rules of python and everything. 

But I'm pretty sure about the logic and flow of this process, just need some tweaks and optimization.

I hope (wish, pray) I'm guiding you to the right path :D :D :D

That does seem logical and pretty similar from what I know of java, I'll test what you gave me. Thanks again

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6306332
Share on other sites

Link to post
Share on other sites

Hello all, I am supposed to create a program in Python that gives the output of the something like this

Welcome to the dice roll simulator.

How many sides does the dice have? 4

How many times would you like to roll the dice? 3

** , 2 dots.

**** , 4 dots.

* , 1 dot.

Total value of all rolls:  7

 

I have been working on figuring out this code for a while.  The only guideline I'm supposed

to follow is using diceRoll = random.randint(1, diceSides) to get the final output.  However,

I can't even think of a place to start.  could anyone give me some pointers on what to do?

Any help is appreciated

 

-CJ

 

Edit: I am also supposed to do this using a while loop

You'll want something like this that I've put together just now. I'll add comments later to explain what everything does. Hope this is of use to you.

 

http://pastebin.com/J60NtVAA

How to create a strong password

Size does not matter; it's how you use it

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6307043
Share on other sites

Link to post
Share on other sites

You'll want something like this that I've put together just now. I'll add comments later to explain what everything does. Hope this is of use to you.

 

http://pastebin.com/J60NtVAA

He needs to do it using while loop.

But I think what you did is better that mine.

That if-else i did on "*" printing is not needed.

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6307079
Share on other sites

Link to post
Share on other sites

Hello all, I am supposed to create a program in Python that gives the output of the something like this

Welcome to the dice roll simulator.

How many sides does the dice have? 4

How many times would you like to roll the dice? 3

** , 2 dots.

**** , 4 dots.

* , 1 dot.

Total value of all rolls:  7

 

I have been working on figuring out this code for a while.  The only guideline I'm supposed

to follow is using diceRoll = random.randint(1, diceSides) to get the final output.  However,

I can't even think of a place to start.  could anyone give me some pointers on what to do?

Any help is appreciated

 

-CJ

 

Edit: I am also supposed to do this using a while loop

OK, here's a better version, with comments explaining what's going on. Hope this helps :)

 

http://pastebin.com/GGmUBvbC

 

EDIT: You'll need to implement a way to catch the error thrown up if a user doesn't enter a number for either question, then ask the question again. You could even try and interpret when numbers are entered as words. Currently, int(raw_input("")) throws up an error for anything but a number.

 

EDIT 2: You didn't say whether you are using Python 2 or Python 3. My code is for Python 2. For Python 3, replace raw_input with input.

How to create a strong password

Size does not matter; it's how you use it

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6307739
Share on other sites

Link to post
Share on other sites

Here's something similar to/an alternative to dom1310df's thorough example, just for the sake of showing a slightly different approach (dom's solution is about as clean as you could hope for).  It should be functionally equivalent, but it's a slightly different approach using a list to store the results of your rolls.  This makes it easy to do things like find the average of your rolls, get histogram data, etc if you so choose.  The printed output is also slightly different, just to make things line up vertically a bit nicer with the pips, and using string formatting for summing up the rolls.

 

This is for Python 3.  You'd have to change the print() functions to statements and change input() to raw_input() to use it with Python 2.

import randomnumsides = int(input("How many sides should the dic have? "))numrolls = int(input("How many times would you like to roll the die? "))# List of all rollsrolls = []counter = 0# Generate the list. As the list is generated, print each roll.while counter < numrolls:    rolls.append(random.randint(1, numsides))    print(rolls[-1], rolls[-1] * "*", sep="\t")    counter += 1print("Total value of all rolls is %i" %sum(rolls))

I really wanted to use some list comprehension here (generate the rolls list using rolls = [random.randint(1, numsides) for i in range(numrolls)]) but that, sadly, would have kind of made the while-loop trivial.

 

You could also change the while statement to while len(rolls) < numrolls, but this won't scale too nicely for a large number of rolls.  But, it's a different approach.

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6311134
Share on other sites

Link to post
Share on other sites

OK, here's a better version, with comments explaining what's going on. Hope this helps :)

 

http://pastebin.com/GGmUBvbC

 

EDIT: You'll need to implement a way to catch the error thrown up if a user doesn't enter a number for either question, then ask the question again. You could even try and interpret when numbers are entered as words. Currently, int(raw_input("")) throws up an error for anything but a number.

 

EDIT 2: You didn't say whether you are using Python 2 or Python 3. My code is for Python 2. For Python 3, replace raw_input with input.

This solved the problem.  Although today my professor basically gave the answer because a bunch of people complained that the directions were unclear.  Thank you all for your help!!

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/470247-python-dice-roll-help/#findComment-6311662
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×