如何使用Python指定骰子游戏中的条件?

2024-09-30 01:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个刚开始学习代码的新手。我决定用Python自己构建一个垃圾游戏。我有一个使用随机模块编写的简单程序。它工作得很好,生成了我想要的数字。但是,我想让我的程序知道在第一卷上滚动7、11、2、3和12与在随后的卷上滚动的区别。现在每卷都是全新的。我可能没有很好地表达我希望实现的目标,但我希望会有一些反馈。我的代码如下。也可以随意评论你注意到的其他事情。也许我试着在编码3周后变得太复杂了

#dice roll
import random
while True:
    diceOutput=random.randint(1,6)
    diceOutput2=random.randint(1,6)
    diceRoll=input('Please type \'r\' to roll')

    if diceRoll== 'r':
        print(diceOutput)
        print(diceOutput2)

    else:
        print('Only type \'r\'')

Tags: 模块代码程序游戏type数字random垃圾
1条回答
网友
1楼 · 发布于 2024-09-30 01:24:51

这里有一种将掷骰保存在列表中的方法,可以在以后的掷骰中进行检查,还可以通过抽样检查来确定掷骰是否是赢家

我认为这可能有助于让你继续完成你想要实现的目标

#dice roll
import random

rolls = []

while True:
    diceRoll=input('Please type \'r\' to roll')

    if diceRoll != 'r':
        print('Only type \'r\'')
        continue

    diceOutput = random.randint(1,6)
    diceOutput2 = random.randint(1,6)

    print("dice 1:", diceOutput)
    print("dice 2:", diceOutput2)
    total_roll = diceOutput + diceOutput2

    if(total_roll == 7 or
       total_roll == 11):
        print('you win!')
        break

    rolls.append(total_roll)

相关问题 更多 >

    热门问题