python - 有什么方法可以将随机值相加并打印出来吗?
问题描述
显然需要随机,但它使收集值变得更加困难。=) 这是不必要的评论。
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()
解决方案
查找字典将大大简化代码。
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
推荐阅读
- java - 访问 Azure Key Vault 以进行本地开发
- python - OSError发送电子邮件django
- ibm-cloud - IBM Watson Assistant:用户输入按钮标签为文本,而不是单击按钮
- list - SwiftUI 删除列表填充和行间距?
- javascript - 为什么原型设置为null时__proto__不更新
- openmdao - 当您有多个提升的输入时,如何在promotes_inputs=[] 中应用src_indices?
- python - 如何解决此错误:AttributeError: 'NoneType' object has no attribute 'reply_text'?
- python - 滚动条未出现在 Tkinter 程序中
- scala - Spark 在读取另一个 Spark 流作业写入 S3 的镶木地板文件时挂起
- javascript - 在 Apple Safari 14.0.3 中,Javascript 将不会设置 cookie 的过期时间超过一周