Question

(CO 4 and 6) Create a program with a function named rollDice that will roll a...

(CO 4 and 6) Create a program with a function named rollDice that will roll a dice as many times as the user asks and with a user specified number of sides. The function should return the roll of the die. The program should continue while the user presses Y.   

Sample output:

How many times do you want to roll the die? 5

How many sides does the die have: 6

You rolled 1

You rolled 2

You rolled 3

You rolled 6

You rolled 3

To continue press Y: Y

How many times do you want to roll the die? 3

How many sides does the die have: 4

You rolled 2

You rolled 4

You rolled 1

To continue press Y: N



Specifications:
•         You will need to import random in your code
•         The code to generate a number from 1 to the input sides is: roll = random.randint(1,sides)
•         Use the .upper() command to check upper and lowercase input example: cont = input("To continue press Y: ").upper()


0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random


def rollDice(sides, n):
    for i in range(n):
        print("You rolled " + str(random.randint(1, sides)))


def main():
    while True:
        n = int(input("How many times do you want to roll the die? "))
        sides = int(input("How many sides does the die have: "))
        rollDice(sides, n)
        choice = input("To continue press Y: ")
        if choice.lower() == "n":
            break


main()

Add a comment
Know the answer?
Add Answer to:
(CO 4 and 6) Create a program with a function named rollDice that will roll a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with...

    Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with a function named flipCoin that will flip a coin as many times as the user asks. The flipCoin function should return “heads” or “tails”. The program should continue while the user presses Y. SAMPLE OUTPUT: How many times do you want to flip a coin? 5 You got: tails You got: tails You got: tails You got: tails You got: heads To continue press...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a...

    WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a lot. Pig Dice Game Pig is a simple two player dice game, played with one die. The first player to reach or surpass 50 is the winner. Each player takes a turn rolling the dice. They add to the pot with each roll, having to decide to roll again and increase the pot, or cash out. The risk being they could lose the amount...

  • Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...

    Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time . //these global variable with static variable win use to operate the working of code . //static win, account because two times input given by user to add the diceroll with win number and //account (means balance of user) . static int account = 100; static int win = 0; int bet...

  • Complete each problem separately and perform in python. 1. Create a script that will roll five...

    Complete each problem separately and perform in python. 1. Create a script that will roll five dice and print out their values. Allow the user to roll the dice three times. Prompt them to hit enter to roll. Ex: Hit enter to roll 1,4,3,6,6 Hit enter to roll 3,5,1,2,1 Hit enter to roll 4,3,4,2,6 2. Write a script that will roll five dice (just one time). The user's score will be the sum of the values on the dice. Print...

  • Write a C program that prompts the user for the number for times the user wants...

    Write a C program that prompts the user for the number for times the user wants to flip a coin. Your program will flip a virtual coin that number of times and return the following: 1) The number of times heads occurred. 2) The number of times tails occured. Then take the coin flip program and modify the random number generator to roll a single 6-sided dice.   DO NOT DISPLAY THE RESULTS OF EACH ROLL. 1) Ask the user how...

  • PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll...

    PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll and for the number of sides for the dice. Roll that many n-sided dice and store the values an array. Take the number of sides of the dice and the number of dice as typed inputs from the user. Output should be a comma separated list of the die numbers with no spaces like this: 6,4,1,3,1 Write a script that will simulate the roll...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • I am having some trouble writing this program. In C language Write a program which asks...

    I am having some trouble writing this program. In C language Write a program which asks user 1) number of sets (integer input), followed by 2) number of sides of a dice (integer input) and 3) number of dices (integer input). number of sets mean how many times this process is going to repeat, number of sides mean total number of sides in a dice (usually 6) and number of dices mean how many number of dices we’re going to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT