Chris Ch.
Chris Ch.

Reputation: 11

Lost on part with Python loops with a function to roll combined value of dice

I have to create a function of two randomly rolled dice the return the combined value. Then I have to use a loop to to roll it to give me five different combined values.

The point I keep getting stuck at is defining the loop in terms of the combined roll to get an out come.

I just keep getting the sequence 1 22 333 4444 55555

If you have any suggestions or help its greatly appreciated. Thank you.

Heres the code I've worked out so far

import random

def point():
    rollone = random.randint(1, 6)
    rolltwo = random.randint(1, 6)
    combined_value = dicesum(rollone, rolltwo)    


def dicesum(x, y):
    result = x + y
    return result 


for result in range(1,6):
    for dicerolls in range(result):
        print(result, end='')
    print()
point()

Upvotes: 1

Views: 1032

Answers (3)

davidlowryduda
davidlowryduda

Reputation: 2559

In your code, very little is being printed.

import random

def point():
    rollone = random.randint(1, 6)
    rolltwo = random.randint(1, 6)
    combined_value = dicesum(rollone, rolltwo)
    # Doesn't return anything


def dicesum(x, y):                  # A long way of saying return x+y
    result = x + y                  # shouldn't be a method
    return result 


for result in range(1,6):           # loop one...
    for dicerolls in range(result): # loop two!
        print(result, end='')
    print()
point()                             # Does't output anything, as mentioned above

What you have actually printing is the same as

for how_many_repeats in range(1,6):
    for num in range(how_many_repeats): # Print num how_many_repeats times
        print(num, end='')
    print()                             # Add a blank

The shortest correction to your code might look like

def point():
    rollone = random.randint(1, 6)
    rolltwo = random.randint(1, 6)
    combined_value = dicesum(rollone, rolltwo)
    return combined_value

for roll in range(5):
    print roll, point()

(And I see now this is what Cyber put down).

Upvotes: 0

NaeiKinDus
NaeiKinDus

Reputation: 770

The issue with your code is that you create a loop on a range(n) within a range(1,6), so basically the first time you iterate you only get 1 total loop (the second range), then you create a new range(2) which prints two times the content of your result. During the third iteration, you call range(3) which iterates 3 times your print (and again, your print only outputs the result variable, not the dicerolls).

It indeed gives the result you have. If I wasn't clear enough, replace the line:

print(result, end='')

by the following line:

print('Roll {}, iteration {}'.format(result, dicerolls))

Execute it, and you will have a nicer result.

EDIT: @Cyber's answer is perfect if you want to correct your code. Mine only tries to explain why you got that result :)

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117981

Your point method doesn't return anything after it sums the results. See the below code.

import random

def point():
    rollone = random.randint(1,7)         # sample (1,7) to get values from 1 to 6
    rolltwo = random.randint(1,7)
    combined_value = rollone + rolltwo    # don't need a helper just to add these
    return combined_value                 # return the combined value

def main():
    for roll in range(5):
        print "Roll", roll, "Sum", point()

Testing

>>> main()
Roll 0 Sum 6
Roll 1 Sum 2
Roll 2 Sum 5
Roll 3 Sum 7
Roll 4 Sum 12

Upvotes: 2

Related Questions