如何找到完成蛇梯游戏所需的平均掷骰数?

2023-12-10

编写代码来模拟一名玩家的游戏并计算完成游戏所需的掷骰子次数。应允许用户指定模拟的游戏数量,并且代码应计算每场游戏的平均掷骰子数量。

#Snakes and Ladders simulation

import random #importing the random function to be able to use it later on

counterposition = 0 #setting counterposition and diceroll to 0
currentDiceroll = 0

def diceroll (): #when user rolls the 1-6 dice this does it randomly
    return random.randint (1,6)
userInput = int(input("How many games would you like to play snakes and ladders?"))

for i in range (userInput):
    currentDiceroll = diceroll()
    print("The currentDiceroll is", currentDiceroll)

    if counterposition == 1:                    #all the if statements show what happens if the one player lands on a snake or a ladder
        counterposition = counterposition + 37
    if counterposition == 4:
        counterposition = counterposition + 10
    if counterposition == 9:
        counterposition = counterposition + 22
    if counterposition == 21:
        counterposition = counterposition + 21
    if counterposition == 28:
        counterposition = counterposition + 56
    if counterposition == 51:
        counterposition = counterposition + 16
    if counterposition == 72:
        counterposition = counterposition + 19
    if counterposition == 80:
        counterposition = counterposition + 19
    if counterposition == 17:
        counterposition = counterposition - 10
    if counterposition == 54:
        counterposition = counterposition - 20
    if counterposition == 63:
        counterposition = counterposition - 4
    if counterposition == 64:
        counterposition = counterposition - 4
    if counterposition == 87:
        counterposition = counterposition - 51
    if counterposition == 92:
        counterposition = counterposition - 19
    if counterposition == 95:
        counterposition = counterposition - 20
    if counterposition == 98:
        counterposition = counterposition - 19
    if counterposition >= 100:
        print ("Congratulations end of game")

    counterposition = counterposition + currentDiceroll
    print("the counter position is", counterposition) 

您编写的代码仅掷骰子一次。您需要继续滚动,直到赢得比赛。最好创建一个函数来玩一次游戏并返回所需的骰子数量。

def playGame():
    counterPosition = 0
    numberOfRolls = 0
    while (counterPosition < 100):
        numberOfRolls += 1
        currentDiceroll = diceroll()
        print("The currentDiceroll is", currentDiceroll)
        counterPosition += currentDiceroll
        # your if statements go here
    return numberOfRolls

然后您可以根据需要多次调用该函数。

totalDiceRolls = 0
for i in range(userInput):
    totalDiceRolls += playGame()
avgRolls = totalDiceRolls / userInput
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何找到完成蛇梯游戏所需的平均掷骰数? 的相关文章

  • 将 ical 附件的邮件消息的内容类型设置为“text/calendar; method=REQUEST”

    我正在尝试使用 App Engine 邮件 API 从 App Engine 发送 iCalendar 格式的 ics 文件 这在 GMail 中非常有效 但是 Outlook 无法识别该文件 我认为问题在于内容类型设置为 文本 日历 而不
  • 需要根据数据框中的行号应用不同的公式

    我正在努力在数据框中找到某种移动平均值 该公式将根据正在计算的行数而变化 实际场景是我需要计算Z列 Edit 2 以下是我正在使用的实际数据 Date Open High Low Close 0 01 01 2018 1763 95 176
  • 是否可以在 IPython 控制台中显示 pandas 样式?

    是否可以显示熊猫风格 https pandas pydata org pandas docs stable user guide style html在 iPython 控制台中 Jupyter 笔记本中的以下代码 import panda
  • 如何在Python + Selenium中获取元素的值

    我在我的 Python 3 6 3 代码中得到了这个 HTML 元素 作为 Selenium网页元素当然 span class ocenaCzastkowa masterTooltip style color 000000 alt 5 sp
  • 删除 tkinter 文本默认绑定

    我正在制作一个简单的 tkinter 文本编辑器 但我想要所有默认绑定文本小部件如果可能的话删除 例如当我按Ctrl i它默认插入一个制表符 我制作了一个事件绑定来打印文本框中有多少行 我将事件绑定设置为Ctrl i以及 当我运行它时 它会
  • DataFrame.loc 的“索引器太多”

    我读了关于切片器的文档 http pandas pydata org pandas docs stable advanced html using slicers一百万次 但我从来没有理解过它 所以我仍在试图弄清楚如何使用loc切片Data
  • 如何在Python中手动对数字列表进行排序?

    规格 Ubuntu 13 04 Python 3 3 1 背景 Python的初学者 遇到了这个 手动排序 问题 我被要求做的事情 让用户输入 3 个数值并将它们存储在 3 个不同的变量中 不使用列表或排序算法 手动将这 3 个数字从小到大
  • 尝试校准keras模型

    我正在尝试通过 Sklearn 实现来校准我的 CNN 模型CalibratedClassifierCV 尝试将其包装为KerasClassifier并覆盖预测功能但没有成功 有人可以说我做错了什么吗 这是模型代码 def create m
  • 提高光线追踪命中功能的性能

    我有一个简单的 python 光线追踪器 渲染 200x200 的图像需要 4 分钟 这对于我的口味来说绝对是太多了 我想改善这种情况 几点 我为每个像素发射多条光线 以提供抗锯齿功能 每个像素总共发射 16 条光线 200x200x16
  • 为什么 pip 已经是最新的了却要求我升级?

    我全新安装了 python 3 7 1 64 位 并使用最新的 pyCharm 作为我的 IDE 我在这台机器上没有安装其他 python 我去安装 numpy 并收到以下消息 venv C Users John PycharmProjec
  • 类变量:“类列表”与“类布尔值”[重复]

    这个问题在这里已经有答案了 我不明白以下示例的区别 一次类的实例可以更改另一个实例的类变量 而另一次则不能 示例1 class MyClass object mylist def add self self mylist append 1
  • Django 1.7:如何使用 html/css 文件作为模板发送电子邮件

    从 Django 1 7 开始 可以send email 使用新参数 html message 不幸的是 没有关于如何使用它的全面指南 新手友好 或者至少我找不到它 我需要使发送的电子邮件变得漂亮 因此 我试图弄清楚如何将我的消息包含到 h
  • python 中的 F 字符串前缀给出语法错误[重复]

    这个问题在这里已经有答案了 我有一个名为 method 的变量 它的值是 POST 但是当我尝试运行时print f method method is used 它不断在最后一个双引号处给出语法错误 我找不到它这样做的原因 我正在使用 py
  • 将二进制数据视为文件对象?

    在此代码片段 由另一个人编写 中 self archive是一个大文件的路径并且raw file是以二进制数据形式读取的文件内容 with open self archive rb as f f seek offset raw file s
  • 为数据集生成随机 JSON 结构排列

    我想生成 JSON 结构的许多不同排列作为同一数据集的表示 最好不需要对实现进行硬编码 例如 给定以下 JSON name smith occupation agent enemy humanity nemesis neo 应该产生许多不同
  • 关闭正在运行代码的 IPython Notebook

    怎么运行的 我在 IPython Notebook 中运行了一些代码 一些迭代工作 我不小心关闭了正在运行的笔记本的浏览器 但回到 IPython 仪表板 我发现这个特定的笔记本尚未关闭 所以如果我再次打开笔记本 我会在它正在执行的代码前面
  • 在自定义 keras 层的调用函数中传递附加参数

    我创建了一个自定义 keras 层 目的是在推理过程中手动更改前一层的激活 以下是基本层 它只是将激活值乘以一个数字 import numpy as np from keras import backend as K from keras
  • 如何仅读取 CSV 文件每行的第一列 [重复]

    这个问题在这里已经有答案了 如何在Python中读取CSV文件每行的第一列 我的数据是这样的 1 abc 2 bcd 3 cde 我只需要循环第一列的值 另外 当我在 calc 中打开 csv 文件时 每行中的数据都在同一个单元格中 这正常
  • 如何使用 keras.backend.gradients() 获取梯度值

    我试图获得 Keras 模型的输出相对于模型输入 x 而不是权重 的导数 似乎最简单的方法是使用 keras backend 中的 梯度 它返回梯度张量 https keras io backend https keras io backe
  • 将二进制数转换为包含每个二进制数的数组

    我试图将二进制值转换为每个 1 0 的列表 但我得到默认的二进制值而不是列表 我有一个字符串 我将每个字符转换为二进制 它给了我一个列表 其中每个字符都有一个字符串 现在我试图将每个字符串拆分为值为 0 1 的整数 但我什么也得不到 if

随机推荐