Python Forum
Please help me add all the results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me add all the results
#1
Thumbs Up 
Hey all, Im trying to code dice rolls that all get added together

Two pieces of code here, first bit generates all the numbers and prints then out, the second set of code is me try to get it added all together

please help

#working code
import random


def D4(x):
    
    for d4 in range(x):
        d4 = (random.randint)(1,4)  
        
        print(d4)
#eg
D4(10)
################################################


#my best attempt
import random

def D4(x):

    rolls = 0
    for d4 in range(x):
        d4 = (random.randint)(1,4)  
        rolls = rolls + d4
        
        return rolls
                            
def dicetotals(x):
    print(D4(x))

dicetotals(2)
Larz60+ write Dec-10-2020, 11:33 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
import random

def D4(x):

    rolls = 0
    for d4 in range(x):
        d4 = (random.randint)(1,5)
        rolls +=  d4

    return rolls

dicetotals = D4(4) 

print(dicetotals)
mr_kungfu likes this post
Reply
#3
You had an indentation problem. And stop wrapping things in parenthesis that don't need parenthesis.
import random
 
def D4(count):
    total = 0
    for _ in range(count):
        total += random.randint(1,4)  
    return total
                             
print(D4(2))
You could make a generic die roller that works for any sided dice. This one returns the total and the individual die. If you call it without any arguments it rolls a single six sided die.
import random
 
def roll(sides=6, count=1):
    die = random.choices(range(1, sides+1), k=count)
    return sum(die), die

print(*roll(4, 2))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,190 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,072 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020