我的任务在Python 3.3.2中,它在这里:
为骰子对象创建一个类,该对象可以随机生成一个介于1和6之间的数字并保存该文件。什么问题? Python中没有打印
您需要随机模块和
创建2个骰子对象A和B,并添加值后。
下面是规则
赢=总等于7或11
失去=总等于2,3或12
辊再次=总等于5,6,8,9,10直到7滚动或再次抛出相同的数字。
现在的代码,我已经写了:
import random
class Dice:
'''A class that makes Dice'''
number = random.randint(1,6)
a = Dice
b = Dice
result = a.number + b.number
def resultgiver():
if result == '7':
result == '11'
print('You won! You got ' ,result,'.')
elif result == '2':
result == '3'
result == '12'
print('You lost! You got ' ,result,'.')
elif result == '5':
result == '6'
result == '8'
result == '9'
result == '10'
print('Roll again! You got ' ,result,'.')
elif result == '5':
result == '6'
result == '8'
result == '9'
result == '10'
elif result == '7':
result == '11'
resultgiver()
尝试为'a'和'b'实例化Dice,例如a = Dice()。另请注意,Dice.number将始终保持不变 - 只在导入时评估随机数。你可能想把它放在\ _ \ _ init _ _ \ _中。 – dbn