Reputation: 3
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))
Upvotes: 0
Views: 56
Reputation: 903
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.
Upvotes: 1
Reputation: 1083
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
Upvotes: 0