Python:Lucky Sevens游戏(平均骰子掷骰数)

  • 本文关键字:Lucky Sevens 游戏 Python python
  • 更新时间 :
  • 英文 :


我正在尝试编写一个计数函数,以计算击中0的掷骰数。让它计算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()

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

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