显然需要随机性,但是这使得收集值变得更加困难。 =)这是不必要的评论。
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
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)
我只需要这个数字,这样我就可以在课堂上有一些价值。
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 == '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()
答案 0 :(得分:0)
您的user == 'yes' or 'yep' or 'y' ...
行将始终返回True
,因为or
是低优先级选项,并且非空字符串的布尔值是True
。
我试图清理一下您的代码,这就是我所拥有的
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()
答案 1 :(得分:0)
查找字典将大大简化代码。
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