Python Forum
Polyhedral Dice Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polyhedral Dice Problem
#1
I get these PowerPoints based around certain python coding subjects, it gives us a basic tutorial and then gives us prompts to solve. I tried to make the code based around the question below, however I'm obviously doing it wrong and it comes up with the same syntax [Traceback (most recent call last):
File "main.py", line 30, in <module>
RollDice(D,D4,D8,D10,D12,D20)
TypeError: RollDice() takes 1 positional argument but 6 were given], and possible others. If anyone can give me ideas on where I'm going wrong or what I need to change, please let me know as it will be a huge help, Thank You!

Question:
In role playing and board games it is common to have dice with more or less than six sides. D4, D8, D10, D12, D20 are often used. ​
Write a function that takes one parameter: the number of faces on a dice. The function should return a random roll of the dice.​

Code (Still a beginner):
import random
#Subroutine to generate a random number
random.seed()
D4= int(4)
D8 = int(8)
D10 = int(10)
D12 = int(12)
D20 = int(20) 
#Main Program
D=int(input("Please input the dice number you want from the list: 4, 8, 10, 12, 20. "))
def RollDice(D):
  if D == D4:
    return random.randint(1,4)
    print("Rolled a {}".format(Dice))
  elif D == D8:
     return random.randint(1,8)    
     print("Rolled a {}".format(Dice))
  elif D == D10:
    return random.randint(1,10)
    print("Rolled a {}".format(Dice))
  elif D == D12:
    return random.randint(1,12)
    print("Rolled a {}".format(Dice))
  elif D == D20:
    return random.randint(1,20)
    print("Rolled a {}".format(Dice))
  else:
    print("Sorry the AtN you've given hasn't been an element picked from group 1-2 or isn't an element within that group.")
Dice = RollDice(D)
RollDice(D,D4,D8,D10,D12,D20)
Reply
#2
Why do does your function have if statements? Why does it have print statements? Where does the assignment say you can only have 4, 8, 10, 12 or 20 sided dice? The assignment says the number of sides is passed in as an argument. It does not say you are supposed to check the number of sides against accepted dice.

Why are you doing this:
RollDice(D,D4,D8,D10,D12,D20)
The instructions say the function takes one parameter. You cannot pass 6 parameters.
Reply
#3
And, since the function is to receive one parameter that is the number of sides on the die you should just pass the number, not "D20".
Your function can then be reduced to one line.
rob101 likes this post
Reply
#4
And since it is one line, you wonder why you are writing a function at all.

I think dice are better represented using a class than a function. A Dice class can remember that it is a 6 sided die or a 12 sided die.

Though it doesn't fit the directions of your assignment at all, I would write dice like this:
from random import randint, seed

seed()


class Dice:
    ''' Generate random integers in a range '''
    def __init__(self, sides=6):
        self.sides = sides

    @property
    def roll(self):
        ''' Roll the dieand return the value '''
        self.value = randint(1, self.sides)
        return self.value


class Yahtzee:
    ''' Roll up to 5 six sided dice, like when playing Yahtzee '''
    def __init__(self):
        self.dice = [Dice(6) for _ in range(5)]

    def roll(self, *dice):
        '''
        Roll the dice.  Can specify which dice to roll.  Rolls all dice if no dice are specified.
        Returns list of dice values for all dice.
        '''
        if dice:
            # Roll specified dice.  Dice are specified by index starting with 1.
            for die in dice:
                self.dice[die - 1].roll
        else:
            # Roll all the dice
            for die in self.dice:
                die.roll
        return [die.value for die in self.dice]
Then I could play a game of Yahtzee in Python.
Output:
>>> from dice_games import Yahtzee >>> me = Yahtzee() >>> me.roll() [5, 1, 5, 2, 1] >>> me.roll(2, 4, 5) [5, 4, 5, 2, 3] >>> me.roll(2, 4, 5) [5, 1, 5, 5, 6] >>>
Reply
#5
@DaCodingSpider

It seems to me that you are over-complicating the project and that the answers that you have thus far, could seem a little daunting.

Take it one step at a time: "Write a function that takes one parameter: the number of faces on a dice."

So, a function that takes one parameter and simply returns it, would be:

def myfunction(D): # `D` is an arbitrary name
    # this is a test
    return D

print(myfunction(8))
Can you now see how D could represent the number of faces, regardless of how many (4 6 20 100; it matters not) and how you can use D with the .randint() function?

About that: no need to seed it: just call .randint(start, stop) where start and stop are the minimum and maximum values to be used. Try it with some test values, such as print(random.randint(4, 12)) or print(random.randint(2, 8))

[edit to add: looking back at your code, it seems that you may have a good understanding for the .randint(start, stop) part, seeding aside]

You could reduce the function to one line or even learn about classes, but get the basics down first, as they are essential to your progress.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020