In: Computer Science
In Python write a function that simulates the throwing of any even numbered dice which will be passed on as a parameter: customDice(sides). The function will return a random number rolled for that particular type of dice. Write the program that will utilize the customDice function with the following specification: Ask the user to pick which even-sided dice to use. The program will then roll the dice twice. If the total of both rolls is greater than the number of die sides+1, then they lose. If not, they win. Write statements that prompt the user for the type of dice to use, print the value from the two rolls and a message whether they win or lose. If the user enters an odd number, ask for the input again.
Sample dialogs:
Please choose an even-sided dice: 3
Sorry, it must be an even-sided dice.
Please choose an even-sided dice: 10
Roll 1: 3
Roll 2: 10
Total: 13
You lose (13 > 11)!
Please choose an even-sided dice: 8
Roll 1: 3
Roll 2: 2
Total: 5
You win (5 < 9)!
i hope u will understand the program .. i have commented every statement using '#' for easy understanding..
comment if u have any query...:)
import random
def customdice(side): #we defined customedice function
roll1 = random.randint(1,6) #roll1 stores random numnber generated by dice
roll2 = random.randint(1,6) #roll2 stores random numnber generated by dice
print("Roll 1:",roll1)
print("Roll 2:",roll2)
sum = roll1 + roll2
return sum #it will return total of dices to calling function
def even(side): #checking for user entered even number or not
if side%2==0 :
return side
else:
print("Sorry,it must be an even sided dice")
return
ch= 'y'
while ch == 'y': #it will continue until user don't want
side = int(input("Please choose an even sided dice")) #user input for dice
if side == even(side) : #calling even Function
sum = customdice(side)
print("Total :",sum)
if sum > side+1 : #checking winning strategies
print("You Lose.(",sum,">",side+1,")!")
elif sum == side+1 :
print("Tied try again.(", sum, "=", side+1, ")!")
else :
print("You Won.(", sum, "<", side+1,")!")
ch = input("Enter 'y' to continue or 'n' to Exit") #asking user to continue or not
output :