简体   繁体   English

Python:幸运七人制游戏(掷骰子的平均次数)

[英]Python: Lucky sevens game (Average number of dice rolls)

I'm trying to write a count function, to calculate the number of rolls it takes to hit 0. And it is returning me the random integer of the diceroll + 1, how do I get it to count the number of times the myroll variable occurs.我正在尝试编写一个计数函数,以计算达到 0 所需的滚动次数。它返回给我骰子 + 1 的随机整数,我如何让它计算 myroll 变量的次数发生。

import random


def luckysevens():
    mypot = int(input("Please enter the amount of money you want to in the pot: "))

    while mypot > 0:
        diceroll = random.randint(1, 6)
        print(diceroll)
        myroll = (diceroll + diceroll)
        if myroll == 7:
            mypot = mypot + 4
            print("Your roll was a 7 you earned 4$", mypot)
        else:
            mypot = mypot - 1
            print("Your roll was", myroll, "you lost 1$", mypot)
    if mypot == 0:
        print("Your out of money!")
    sum = 0
    for count in range(myroll + 1):
        sum = sum + count
    print(count)


luckysevens()

If you want to count how many rolls before the loop exits, simply add another counter variable.如果您想计算循环退出前的滚动次数,只需添加另一个计数器变量。 I also am assuming you're rolling a couple dice, so added different random calls for each one.我还假设您正在掷骰子,因此为每个骰子添加了不同的随机调用。

import random

mypot = int(input("Please enter the amount of money you want to in the pot: "))

num_rolls = 0
while mypot > 0:
    die_1 = random.randint(1,6)
    die_2 = random.randint(1,6)

    myroll = die_1 + die_2

    num_rolls += 1 # count rolls

    if myroll == 7:
        mypot = mypot + 4
        print("Your roll was a 7 you earned 4$",mypot)
    else:
        mypot = mypot - 1
        print("Your roll was",myroll,"you lost 1$",mypot)

if mypot == 0:
    print("Your out of money!")

print 'Num rolls: {}'.format(num_rolls) # print rolls

在输入代码时,它显示语法错误是num_roll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM