Steven Schmieg
Steven Schmieg

Reputation: 49

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.

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()

Upvotes: 2

Views: 1943

Answers (1)

LeKhan9
LeKhan9

Reputation: 1350

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

Upvotes: 2

Related Questions