简体   繁体   中英

Python program using class programs to simulate the roll of two dice

My program is supposed to simulate to both simulate the role of a single dice and the role of two dices but I am having issues. Here is what my code looks like:

import random

#Dice class simulates both a single and two dice being rolled
#sideup data attribute with 'one'

class Dice:
    #sideup data attribute with 'one'

    def __init__(self):
        self.sideup='one'
    def __init__(self):
        self.twosides='one and two'

 #the toss method generates a random number
 #in the range of 1 through 6.


    def toss(self):
        if random.randint(1,6)==1:
            self.sideup='one'
        elif random.randint(1,6)==2:
            self.sideup='two'
        elif random.randint(1,6)==3:
            self.sideup='three'
        elif random.randint(1,6)==4:
            self.sideup='four'
        elif random.randint(1,6)==5:
            self.sideup='five'
        else:
            self.sideup='six'
    def get_sideup(self):
        return self.sideup
    def doubletoss(self):
        if random.randint(1,6)==1 and random.randint(1,6)==2:
            self.twosides='one and two'
        elif random.randint(1,6)==1 and random.randint(1,6)==3:
            self.twosides='one and three'
        elif random.randint(1,6)==1 and random.randint(1,6)==4:
            self.twosides='one and four'
        elif random.randint(1,6)==1 and random.randint(1,6)==5:
            self.twosides='one and five'
        elif random.randint(1,6)==1 and random.randint(1,6)==6:
            self.twosides='one and six'
        elif random.randint(1,6)==1 and random.randint(1,6)==1:
            self.twosides='one and one'
    def get_twosides(self):
        return self.twosides






#main function
def main():
    #create an object from the Dice class
    my_dice=Dice()


    #Display the siide of the dice is factory
   print('This side is up',my_dice.get_sideup())

    #toss the dice
    print('I am tossing the dice')
    my_dice.toss()

    #toss two dice
    print('I am tossing two die')
    my_dice.doubletoss()

   #Display the side of the dice that is facing up
    print('this side is up:',my_dice.get_sideup())

    #display both dices with the sides of the dice up
    print('the sides of the two dice face up are:',my_dice.get_twosides())



main()

Here is the output of my program when I run it:

"Traceback (most recent call last): File "C:/Users/Pentazoid/Desktop/PythonPrograms/DiceClass.py", line 79, in main() File "C:/Users/Pentazoid/Desktop/PythonPrograms/DiceClass.py", line 61, in main print('This side is up',my_dice.get_sideup()) File "C:/Users/Pentazoid/Desktop/PythonPrograms/DiceClass.py", line 32, in get_sideup return self.sideup

AttributeError: 'Dice' object has no attribute 'sideup'

What am I doing wrong?

You have two init methods. The second replaces the first, which negates your definition of sideup.

change to:

def __init__(self):
    self.sideup='one'
    self.twosides='one and two'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM