Positional errors


I’m having trouble understanding why I’m getting a positional error in my code to create a Dice Class and what I need to do in order to fix it

Remove the parameter on line 26. The variable is declared locally in the first statement.

But now I’m getting another error that’s saying " TypeError: str returned non-string" after correcting both the errors on Line 26 and Line 18.

Please post your code, not as a screenshot.

# import the random module import random # create the class Dice class Dice: # create the constructor (__init__) method # it takes as input the number sides and if none is specified use 6 # it sets the dice object's number of sides (instance variable) # it sets the list that tracks the rolls to the empty list (instance variable) def __init__(self, side = 6): self.side = side #self.number_of_sides = number_of_sides self.roll_tracker_list = [] # create the __str__ method # it returns "Last roll: value" where value is the last value in the list that tracks the rolls def __str__(self): last_roll_value = print("Last Roll: " + str(self.roll_tracker_list[-1])) return last_roll_value # create the roll method # it randomly picks a value from 1 to the number of sides this dice object has # it adds that value to the end of the list that tracks all the rolls # it returns the value def roll(self): roll_index = random.randint(1,self.side) end_value = self.roll_tracker_list.append(roll_index) return end_value # BONUS # create the print_count_for_num method # it will count how many times the passed number has been rolled and print # number was rolled x times - where number is the number and x is the count # main function def main(): # Create an instance three_sided = Dice(3) print("Three sides dice") # Roll dice 5 times for i in range(5): print(three_sided.roll()) # Print last roll print(three_sided) # Create an instance six_sided = Dice() print("\nSix sides dice") # Roll dice 5 times for i in range(5): print(six_sided.roll()) # Print last roll print(six_sided) # BONUS quiz # Print accumulation #six_sided.print_count_for_num(3) if __name__ == "__main__": main()

I am not allowed to change anything in the main() function.

In line 33 drop the print() and assign the string, or save the trouble and just return the string without assigning it. The caller is printing the return value.

1 Like

Ah ok, I understood that but it’s still giving me a “TypeError: str returned non-string” error even after changing line 33.

Please post your latest code, but not as a Codebyte, just raw code, formatted.

# import the random module
import random
# create the class Dice
class Dice:
    # create the constructor (__init__) method
    # it takes as input the number sides and if none is specified use 6
    # it sets the dice object's number of sides (instance variable)
    # it sets the list that tracks the rolls to the empty list (instance variable)
    def __init__(self, side = 6):
        self.side = side
        self.roll_tracker_list = []
    # create the __str__ method
    # it returns "Last roll: value" where value is the last value in the list that tracks the rolls
    def __str__(self):
        return str("Last Roll: " + str(self.roll_tracker_list[-1]))
    # create the roll method
    # it randomly picks a value from 1 to the number of sides this dice object has
    # it adds that value to the end of the list that tracks all the rolls
    # it returns the value
    def roll(self):
        roll_index = random.randint(1,self.side)
        end_value = self.roll_tracker_list.append(roll_index)
        return end_value
    # BONUS
    # create the print_count_for_num method
    # it will count how many times the passed number has been rolled and print 
    # number was rolled x times - where number is the number and x is the count
# main function
def main():
    # Create an instance
    three_sided = Dice(3)
    print("Three sides dice")
    # Roll dice 5 times
    for i in range(5):
       print(three_sided.roll())
    # Print last roll
    print(three_sided)
    # Create an instance
    six_sided = Dice()
    print("\nSix sides dice")
    # Roll dice 5 times
    for i in range(5):
       print(six_sided.roll())
    # Print last roll
    print(six_sided)
    # BONUS quiz
    # Print accumulation
    #six_sided.print_count_for_num(3)
if __name__ == "__main__":
    main()

I was able to correct the TypeError and get the program to run but the output isn’t correct.

Keep at it. Watching hockey, distracted. Will read and get back to you, presently.

It is funny we didn’t catch it earlier… Is there such a thing, or even the possibility of a three sided die? In the Platonic world, no. The fewest number of sides, apart from a coin (2) is .the Platonic solid, a tetrahedron. We cannot treat dice like they can be probabilisticly anything. If there are sides, then there are norms, and those are the Platonic solids. Shapes in which all sides are shaped the same. They also exhibit equal probability of which face will be on top (or bottom) when tossed, similar again to the coin toss.

You’ve really only got 4, 8, 12 and 20 faces to work with in this realm of equal probability on a toss. Anything else is contrived.

Thank you for all your help, I was able to figure it out!

1 Like