import random
def Dice():
num_dice = raw_input("How many dice are you rolling? ")
user_dice = raw_input("How many sides do you want on your dice? ")
user_diceint = int(user_dice)
dice_rolling1 = random.randint(0, user_diceint)
print dice_rolling, "was your dice roll."
if user_dice > 0 and user_dice.isdigit():
print "This is correct."
if len(num_dice) <= 0:
num_dice = 2
print num_dice, "is the number of dice you rolled"
print num_dice * user_dice, "is your total roll"
else:
print "Number of Dice has an error"
else:
print "This is not a number."
So I am new to coding/programming and I thought I would try my hand at doing a Dice roll generator. So in the code I have num_dice which is the number of dice("duh"). But I was wondering how would I do a different randint for each of the dice. So say someone enters '5' dice. How would I get my code to generate 5 different randomints to simulate 5 actual dice being rolled? Would some kind of loop work for this? Please excuse the bad code, I am new and it's still being worked on! Thank you.
You can put a for loop in your Dice function to handle that
def Dice():
num_dice = int(raw_input("How many dice are you rolling? "))
user_dice = int(raw_input("How many sides do you want on your dice? "))
for roll in range(1, num_dice+1):
dice_roll = random.randint(1, user_dice)
print "Roll number", roll, "was: ", dice_roll
Example
num_dice = 5 # 5 rolls
user_dice = 8 # 8 sided dice
Output
Roll number 1 was: 2
Roll number 2 was: 1
Roll number 3 was: 8
Roll number 4 was: 4
Roll number 5 was: 6
You can do that by creating a list of rolling dice num_dice times:
list_of_values = [random.randint(0, user_diceint) for _ in range(num_dice)]
Your Code
def Dice():
num_dice = raw_input("How many dice are you rolling? ")
user_dice = raw_input("How many sides do you want on your dice? ")
user_diceint = int(user_dice)
list_of_values = [random.randint(0, user_diceint) for _ in range(num_dice)]
print list_of_values, "were your dice rolls."
Related
Im trying to make a loop where I start with 5 random dice rolls. the values 1,3,4, and 6 are added up for your score, while 2s and 5s are not counted and subtract the combined amount of dice rolled with 2s and 5s from the amount of dice rolled the next turn. I can't quite figure out how to get the sum of only the dice that rolled 1,3,4,6 and roll a lesser amount of dice the next turn based off ho many dice were 2 or 5
#Stuck in the Mud Dice Game
Exit = 0
Rolls = int(5)
score = 0
def RollDice():
totalsum = 0
for die in range(0, Rolls):
number = random.randint(1,6)
print("die", die + 1, ":", number)
totalsum += number
print("score: ", totalsum)
def Menu():
print("1. Roll Dice")
print("2. Exit Program")
print()
choice = int(input("Enter option here: "))
if(choice == 1):
RollDice()
if(choice == 2):
Exit()
Menu()
In the for-loop, use an if statement:
if number==2 or number==5:
Rolls -= 1
else:
totalsum += number
I suppose that you also need to start your function RollDice like this:
def RollDice():
global Rolls
...
otherwise, it might only locally (i.e., within the function) assign a new value to the variable.
Or do you mean the number of Rolls should be different when you re-start the program? This can only be done by writing the value to a file.
I need help condensing my Yahtzee code. I'm familiar with loops, but I can't seem to figure out how to do it without wiping my roll each turn. It's set up to roll 5 random dice and then you choose which you want to keep. It will then roll what's left for three total rolls. I thought of making a placeholder list, but I can't make it work that way either.
Dice = [‘⚀ ’,‘⚁ ‘,’ ⚂ ‘,’ ⚃ ‘,’ ⚄ ‘,’ ⚅ ‘]
turns = 3
#first roll
first_roll = []
for _ in range(5):
roll = random.choice(dice)
first_roll.append(roll)
print(first_roll)
turns -= 1
print(f'You have {turns} turns left.')
keep = input("Which dice would you like to keep? 1 - 5: ").split()
clear()
#second roll
second_roll = []
for i in keep:
x = int(i)
j = first_roll[x-1]
second_roll.append(j)
remainder = 5 - len(second_roll)
for _ in range(remainder):
roll = random.choice(dice)
second_roll.append(roll)
print(second_roll)
turns -= 1
print(f'This is your last turn.')
keep = input("Which dice would you like to keep? 1 - 5: ").split()
clear()
#third roll
third_roll = []
for i in keep:
x = int(i)
j = second_roll[x-1]
third_roll.append(j)
remainder = 5 - len(third_roll)
for _ in range(remainder):
roll = random.choice(dice)
third_roll.append(roll)
print(third_roll)
So I'm not sure if this is exactly what you're looking for, but I created a function that lets you continuously roll for X turns and the dice pool gets progressively smaller like your code shows. I thoroughly commented the code to hopefully give you a good explanation to what I'm doing.
import random
#Dice characters
dice = ['1','2','3','4','5','6']
#Number of turns player has
turns = 3
#This is the group of dice kept by the player
kept_dice = []
def roll():
#This is the group of dice randomly chosen
rolled_dice = []
#Generate up to 5 random dice based on how many have been kept so far
for _ in range(5 - len(kept_dice)):
#Append random dice value to rolled_dice array
rolled_dice.append(random.choice(dice))
#Print roll group
print(rolled_dice)
dice_to_keep = int(input("Which dice would you like to keep? 1 - 5: "))
kept_dice.append(rolled_dice[dice_to_keep-1])
while turns != 0:
#Let the user know how many turns they have left
print(f'You have {turns} turns left.')
#Roll dice
roll()
#Subtract 1 turn
turns -= 1
#After all turns, show the kept dice:
print("Kept dice:")
print(kept_dice)
I am 100% new to coding. This is my first semester in IT and Python is making 0% sense to me at all. This is the assignment I have been given:
Code this program as a while statement,
then create it a second time as a for statement
Ask the user how many times she will roll the 2 dice.
Simulate the dice roll that many times by generating 2 random numbers between 1 and 6
Immediately after each roll display the total of the roll
After all of the rolls calculate and display:
-the total of all of the rolls
-the average of all of the rolls
Use meaningful variable names
Comment the main sections of your code and any lines that aren't self-explanatory
Your program output should look like this:
How many times will you roll the dice? 2
You rolled a 1 and a 5
You rolled a 3 and a 6
Your total for all of the rolls is 15
You average for all of the rolls is 7.5
This is what I have so far, but I have spent hours reading our text-book chapters and watching recordings of the lecture and I can not figure out the rest. Please help.
from random import randint
numRolls= int(input("How many times will you roll the dice? "))
for diceRolls in range(numRolls):
d1 = randint(1,6)
d2 = randint(1,6)
print("You rolled a", d1, "and a",d2)
print("Your total for all of the rolls is", )
print("Your average for all of the rolls is", )
Without using lists and additional memory:
from random import randint
numRolls = int(input("How many times will you roll the dice? "))
s = 0
for roll in range(numRolls):
d1 = randint(1, 6)
d2 = randint(1, 6)
s += d1 + d2
print("You rolled a", d1, "and a", d2)
print("Your total for all of the rolls is", s)
print("Your average for all of the rolls is", s / numRolls)
You can use a list to store the data. You can also use the sum function to get the sum of all the values in the list, and use sum(allDiceRolls)/len(allDiceRolls) to get the average
Code:
from random import randint
numRolls = int(input("How many times will you roll the dice? "))
allDiceRolls = []
for diceRolls in range(numRolls):
d1 = randint(1,6)
d2 = randint(1,6)
allDiceRolls.append(d1)
allDiceRolls.append(d2)
print("You rolled a", d1, "and a",d2)
print("Your total for all of the rolls is", sum(allDiceRolls))
print("Your average for all of the rolls is", sum(allDiceRolls)/len(allDiceRolls))
So i'm stuck with this problem, how can i add something within a function and print it from out of the function
import random
def dieRoll():
sum = 0
for i in range (die-1):
dieThrow = random.randrange( 1, sides+1)
sum += dieThrow
print(dieThrow)
print("That totals: ", sum)
return i
players = int(input("How many players are rolling dice? "))
die = int(input("How many dice does each player roll? "))
sides = int(input("How many sides does each die have? "))
for a in range(1, players+1):
print("Player",a, "rolled: ")
print(dieRoll())
it's supposed to look something like:
Player 1 rolled:
5
4
9
that totals: 18
but i'm getting something like:
Player 1 rolled:
5
4
that totals: 9
9
i feel like the answer is right in front of me but i just can't see it
it's because you print what the function return in the end, if you want such output you have two choices :
print(i)
return("That totals: ", sum)
or the other choice :
print(i)
print("That totals: ", sum)
#replace print(dieRoll())
dieRoll()
The problem is the line
for i in range (die-1):
Try this instead:
for i in range(0, die):
The thing is, the range function by default starts at 0 and "stops" when it gets to the last number (does not execute it), meaning you are not geting to do that last dice roll you expected.
Hope it helps.
This is the problem I have: Write a function roll dice that takes in 2 parameters - the number of sides of the die, and the number of dice
to roll - and generates random roll values for each die rolled. Print out each roll and then return the string
“That’s all!” An example output
>>>roll_dice(6,3)
4
1
6
That's all!
This is the code I have so far using a normal roll dice code:
import random
min = 1
max = 6
roll_dice = "yes"
while roll_dice == "yes":
print random.randint(min,max)
print random.randint(min,max)
print "That's all"
import sys
sys.exit(0)
Try this:
def roll_dice(sides, rolls):
for _ in range(rolls):
print random.randint(1, sides)
print 'That\s all'
This uses a for loop to loop rolls amount of times and prints a random number between 1 and sides each loop.
import random
def roll_dice(attempts,number_of_sides):
for i in range(attempts):
print(random.randint(1, number_of_sides))
print("thats all")
In a more advanced, you can have two parameters where you ask the users input to set the values and it will run the functions and give the numbers rolled, the sum and ask if the user wants to try again.
#Import 'random' module
import random
def main():
#Get values from user to determine slides_per_dice and number_of_die.
sides_per_die = get_value(1,50, 'How many sides should the dice to have
(1-50): ', 'That is not a correct response, enter a value between 1 and
50')
number_of_dice = get_value(1,10, 'How many dice should be rolled (1-10):
', 'That is not a correct response, enter a value between 1 and 10')
#Simulate rolling specified number of dice with specified sides on each
dice.
outcome = rolling_dice (sides_per_die, number_of_dice)
#Asking user if they would like to roll again.
roll_again = input("Would you like to play again (y/n): ") .lower()
while roll_again != 'n':
if roll_again =='y':
main()
else:
print('Error: please enter "y" or "n".')
roll_again = input("Would you like to play again (y/n): ")
#Final message if user ends the game.
print('Thanks for playing.')
#Asking user for side_per_dice and number_of_nice input.
def get_value(lower_limit, upper_limit, prompt, error_message):
number = int(input(prompt))
while number < lower_limit or number > upper_limit:
print(error_message)
number = int(input(prompt))
return number
#Determining the outcome of the rolls and summing up total for all rolls.
def rolling_dice (sides_per_die, number_of_die):
roll_sum = 0
print('Rolling dice .....')
for roll in range (number_of_die):
result = random.randint(1, sides_per_die)
roll_sum += result
print(result, end = " ")
print(f'\nThe sum of all dice rolled is {roll_sum}')
main()