0

New to python here, trying to make a simple dice roller, created a func() that returns 3 variables after user input. Is there any way to make one function for the creation of the roll? tried with range but had no luck

import random

def user_input():

    y = int(input("Number of yellow dice to roll? : "))
    g = int(input("Number of Green dice to roll? :"))
    b = int(input("Number of Black dice to roll? :"))
    return y, g, b

y, g, b = user_input()


for n in range(y):
    print("Yellow Dice",n+1,":",random.randint(1,6))

for n in range(g):
    print("Green Dice",n+1,":",random.randint(1,6))

for n in range(b):
    print("Black Dice",n+1,":",random.randint(1,6))
3
  • 1
    what is the thing that you want to achieve, can you provide an input/output example? Commented Oct 2, 2019 at 1:39
  • You could create a function which takes color and number of rolls as parameters and call it three times. Commented Oct 2, 2019 at 1:42
  • I want to create a function that receives the three user input values and generates the corresponding dice roll. Commented Oct 2, 2019 at 1:59

2 Answers 2

1

As what @MichaelButscher suggested in the comments, you can create a function that takes number and color as parameters like this:

def one_func(num,color):
    for i in range(num):
        print(f"Dado {color} {i+1} : {random.randint(1,6)}")

Then, call this function in your texto() function three times.

def texto():
    a = int(input("Dados Amarillos que deseas tirar: "))
    v = int(input("Dados Verdes que deseas tirar: "))
    n = int(input("Dados Negros que deseas tirar: "))

    one_func(a,"Amarillo")    
    one_func(v,"Verde")
    one_func(n,"Negro")

Note: function names referred to post-edit question. I also saw the error that wrong variable naming caused you from that post-edit question.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, yea edited the post because maybe language wise it wasn't clear what i was trying to achieve. but your post made it clear thx!
0

Depend on what you want to archive and a,v,n values (must be the same range). If a,v,n are not the same range, you should keep your code like that.

This is just an example for you.

>>>for a,b,c in range(3), range(1,4), range(2,5):
...    print(a,b,c)

0 1 2
1 2 3
2 3 4   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.