Pregunta

I was wondering how I could use the return value(s) from the functions and divide the value from the result from if number == 12 from if number == 4. Or would I need to make separate functions for each dice?

import random

def dice_roll(number):
    if number == 12:
        number = random.randint(1,12)
        print(number)
        return number
    elif number == 6:
        number = random.randint(1,6)
        print(number)
        return number
    else:
        number == 4
        number = random.randint(1,4)
        print(number)
        return number

print("12 sided")
print("6 sided")
print("4 sided")

while True:
   dice_roll(int(input("Which dice would you like to roll? --> ")))
   doRepeat=input("Go again? --> ")
   if doRepeat == "no":
      break         
¿Fue útil?

Solución

I think what you are describing is something like this:

rolls = {4: [], 6: [], 12: []} # dictionary to hold rolls
while True:
    sides = int(input("Which dice would you like to roll? --> ")) # store sides
    rolls.get(sides, rolls[4]).append(dice_roll(sides)) # roll and add to dict
    doRepeat=input("Go again? --> ")
    if doRepeat == "no":
        break 

As I noted for your previous question, though, you can make the whole thing more flexible (beyond just 4, 6 and 12):

def dice_roll(sides):
    number = random.randint(1, sides)
    print(number)
    return number

rolls = {}
while True:
    sides = int(input("Which dice would you like to roll? --> "))
    rolls.setdefault(sides, []).append(dice_roll(sides))
    ...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top