0

Random is obviously needed, but it makes it harder to gather the values. =) This is an unnecessary comment.

import random
class Die:
    def __init__ (self, face, face_value):
        self.face = face
        self.face_value = face_value
    def roll(self):

I have tried making the class do this twice, but for some reason it does not work.

        self.face = random.randint(1, 6)
        if self.face == 1:
            face = ( ' ------\n|       |\n|   o   |\n|       |\n ------')
            num = 1
        elif self.face == 2:
            face =  (' ------\n|       |\n| o   o |\n|       |\n ------')
            num = 2
        elif self.face == 3:
             face =  (' ------\n|   o   |\n|   o   |\n|   o   |\n ------')
            num = 3
        elif self.face == 4:
            face =  (' ------\n| o   o |\n| o   o |\n|       |\n ------')
            num = 4
        elif self.face == 5:
            face =  (' ------\n| o   o |\n| o   o |\n|   o   |\n ------')
            num = 5
        elif self.face == 6:
            face = (' ------\n| o   o |\n| o   o |\n| o   o |\n ------' )
            num = 6
        return print(face)
 **My goal is to add up the two random values that I get from the Die class, I have tried to put two rolls in that class, but then it has the same output still. **   
from DieClass import Die
user = input('Do you want to play this Dicegame?')
num = random.randint(1,6)

I only need this num so that I have some value to put into the class.

if num == 1:
    face = ( ' ------\n|       |\n|   o   |\n|       |\n ------')
elif num == 2:
    face =  (' ------\n|       |\n| o   o |\n|       |\n ------')
elif num == 3:
    face =  (' ------\n|   o   |\n|   o   |\n|   o   |\n ------')
elif num == 4:
    face =  (' ------\n| o   o |\n| o   o |\n|       |\n ------')
elif num == 5:
    face =  (' ------\n| o   o |\n| o   o |\n|   o   |\n ------')
elif num == 6:
    face = (' ------\n| o   o |\n| o   o |\n| o   o |\n ------' )


class Dicegame(Die):
    def __init__ (self, name):
        self.name = name

    def play(self):
        Die1 = Die(face, num)
        return Die1.roll()

I am trying to make this code more efficient, if you have any suggestions tell me

if user == 'yes' or 'yep' or 'y' or 'Yes' or 'YES!' or 'YES' or 'Yurp' or 'Yeppers' or 
'si'or'1':
    while user != 5:
        user = input('Press 1 to roll, 2 to quit.')
        if user == '1':
            Dice1 = Dicegame('name')
            Dice1.play()
            Dice2 = Dicegame('bob')
            Dice2.play()
            print('')
            print('Thanks for playing!')
            print('')
        elif user == '2':
            print('Thanks for playing!')
            print('')
            exit()
Bob
  • 21
  • 4
  • Look at your title.... just make a break and you'll find out your issues. – Yvain May 28 '20 at 19:03
  • Please ask specific questions separately, not in-line with the code; and make sure your code is formatted properly by adding an *extra* four spaces in front of *each* line of code (select the *entire* code block and hit the `{}` button). If something doesn't work, explain exactly what you expected to happen, vs what actually does happen. – Karl Knechtel May 28 '20 at 19:04
  • Anyway, when you speak of "adding up the two die rolls", it's not at all clear to me which ones you mean. – Karl Knechtel May 28 '20 at 19:05
  • 1
    As an aside, `if user == 'yes' or 'yep' or 'y' or 'Yes' or 'YES!' or 'YES' or 'Yurp' or 'Yeppers' or 'si'or'1':` **does not do what you want**. Please read https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true – Karl Knechtel May 28 '20 at 19:07

2 Answers2

0

Your user == 'yes' or 'yep' or 'y' ... line will always return True because or is a low-priority option, and a non-empty string's boolean value is True.

I tried to clean your code up a bit, here's what I have

import random

class Die:
    def __init__ (self, face, face_value):
        self.face = face
        self.face_value = face_value
    def roll(self):
        self.face = random.randint(1, 6)
        if self.face == 1:
            face = ( ' ------\n|       |\n|   o   |\n|       |\n ------')
            num = 1
        elif self.face == 2:
            face =  (' ------\n|       |\n| o   o |\n|       |\n ------')
            num = 2
        elif self.face == 3:
            face =  (' ------\n|   o   |\n|   o   |\n|   o   |\n ------')
            num = 3
        elif self.face == 4:
            face =  (' ------\n| o   o |\n| o   o |\n|       |\n ------')
            num = 4
        elif self.face == 5:
            face =  (' ------\n| o   o |\n| o   o |\n|   o   |\n ------')
            num = 5
        elif self.face == 6:
            face = (' ------\n| o   o |\n| o   o |\n| o   o |\n ------' )
            num = 6
        print(face)
        return num

user = input('Do you want to play this Dicegame? ')

num = random.randint(1, 6)
if num == 1:
    face = ( ' ------\n|       |\n|   o   |\n|       |\n ------')
elif num == 2:
    face =  (' ------\n|       |\n| o   o |\n|       |\n ------')
elif num == 3:
    face =  (' ------\n|   o   |\n|   o   |\n|   o   |\n ------')
elif num == 4:
    face =  (' ------\n| o   o |\n| o   o |\n|       |\n ------')
elif num == 5:
    face =  (' ------\n| o   o |\n| o   o |\n|   o   |\n ------')
elif num == 6:
    face = (' ------\n| o   o |\n| o   o |\n| o   o |\n ------' )

class Dicegame(Die):
    def __init__ (self, name):
        self.name = name

    def play(self):
        Die1 = Die(face, num)
        return Die1.roll()

if user in ('yes', 'yep', 'y', 'Yes', 'YES!', 'YES', 'Yurp', 'Yeppers', 'si', '1'):
    while user != '5': # what is the purpose of this line?
        user = input('Press 1 to roll, 2 to quit. ')
        if user == '1':
            Dice1 = Dicegame('name')
            a = Dice1.play()
            Dice2 = Dicegame('bob')
            b = Dice2.play()
            print(f'Your total is {a+b}')
            print('Thanks for playing!')
            print('')
        elif user == '2':
            print('Thanks for playing!')
            print('')
            exit()
Eric Jin
  • 3,836
  • 4
  • 19
  • 45
0

A lookup dictionary will simplify the code a whole lot.

import random

FACES = {
    1: " ------\n|       |\n|   o   |\n|       |\n ------",
    2: " ------\n|       |\n| o   o |\n|       |\n ------",
    3: " ------\n|   o   |\n|   o   |\n|   o   |\n ------",
    4: " ------\n| o   o |\n| o   o |\n|       |\n ------",
    5: " ------\n| o   o |\n| o   o |\n|   o   |\n ------",
    6: " ------\n| o   o |\n| o   o |\n| o   o |\n ------",
}

YES_REPLIES = {
    'yes', 'yep', 'y', 'Yes', 'YES!', 'YES', 'Yurp', 'Yeppers', 'si', '1'
}


class Die:
    def __init__(self, face, face_value):
        self.face = face
        self.face_value = face_value

    def roll(self):
        num = self.face = random.randint(1, 6)
        face = FACES[num]
        print(face)
        return num


class Dicegame(Die):
    def __init__(self, name):
        self.name = name

    def play(self):
        num = random.randint(1, 6)
        face = FACES[num]

        Die1 = Die(face, num)
        return Die1.roll()


user = input('Do you want to play this Dicegame? ')
if user in YES_REPLIES:
    while True:
        user = input('Press 1 to roll, 2 to quit. ')
        if user == '1':
            Dice1 = Dicegame('name')
            a = Dice1.play()
            Dice2 = Dicegame('bob')
            b = Dice2.play()
            print(f'Your total is {a+b}')
            print('Thanks for playing!')
            print('')
        elif user == '2':
            print('Thanks for playing!')
            print('')
            break