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

时间:2019-03-04 21:59:51

标签: python

我正在尝试编写一个计数函数,以计算击中0所需的掷骰数。它返回给我diceroll的随机整数+ 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()

1 个答案:

答案 0 :(得分:2)

如果要计算循环退出之前的滚动数,只需添加另一个计数器变量。我还假设您正在掷几个骰子,因此为每个骰子添加了不同的随机调用。

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