Python Forum

Full Version: Multiple return from function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,
Just started with python and is trying to create a dice generator for a specific RPG-game.

I am trying to get an answer back from a function that calculated the outcome.
But I cant get it to work.

Getting the follwing error:

Error:
Traceback (most recent call last): File "/Users/andreas.freij/PycharmProjects/Forbidden Lands Dice Generator/main.py", line 27, in <module> result = roll_dice() TypeError: roll_dice() missing 5 required positional arguments: 'number_of_roll', 'dice_type', 'sides', 'result_list', and 'damage' Process finished with exit code 1
I guess it should be possible to feed a function with some values, and get some other variable back. Right?
It is the RETURN, RESULT in the end of eah code snippets that do not work, and generate the issue.

This is the code:

Main.py

from dice import roll_dice
if number_of_Base != "0":
    roll_dice(int(number_of_Base), Base.dice_type, Base.sides,Base.result_list, Base.damage)
    result = roll_dice()
dice.py

def roll_dice(number_of_roll, dice_type, sides, result_list, damage):
    victory_points = 0
    skulls = 0
    import random
    for x in range(int(number_of_roll)):
        dice_outcome = random.randint(1,int(sides))
        victory_points = victory_points + int(result_list[(dice_outcome)-1])
        if dice_outcome == 1 and damage is True:
           skulls = skulls + 1
    return victory_points,skulls
I have also tried:
victory_points, skulls = roll_dice()
But with the same result...

Please advise!
try

result = roll_dice(int(number_of_Base), Base.dice_type, Base.sides,Base.result_list, Base.damage)
That works,
BUT returns the wrong parameters.

I want to get back: victory_points and skulls from the function. That is what it is designed to calculate.
(Mar-22-2021, 05:26 PM)Grimmar Wrote: [ -> ]BUT returns the wrong parameters.
So, your function does not work as expected - check your code logic.
(Mar-22-2021, 05:26 PM)Grimmar Wrote: [ -> ]I want to get back: victory_points and skulls from the function. That is what it is designed to calculate.
and that is what you get from the function
What others are implying, but not saying outright, is that your function returns the values you want. The error you have is either because you are not providing the correct arguments when you call the function:
victory_points, skulls = roll_dice()
or you ignore the return results.
roll_dice(int(number_of_Base), Base.dice_type, Base.sides,Base.result_list, Base.damage)
Since many of the args to roll_dice() are attributes of Base, why not just pass Base as an argument? Then the calling code looks like this:
victory_points, skulls = roll_dice(int(number_of_base), Base)
And the function looks like this:
def roll_dice(number_of_rol), base):
    victory_points = 0
    skulls = 0
    #  import random  <- Do not hide imports inside a function
    for x in range(number_of_roll):
        dice_outcome = random.randint(1, base.sides)
        victory_points = victory_points + base.result_list[dice_outcome-1])
        if dice_outcome == 1 and damage is True:
           skulls = skulls + 1
    return victory_points, skulls
Thank you @deanhystad for a very constructive answer.
I have changed accordingly and policed the code som more, but I still receiving the same error.
If you or someone could explain what I am doing wrong, I would be very grateful.
I have been looking at code examples at some beginner sites, but for me it looks ok.


main.py full file

from dice import Dice
base = Dice("Grundegenskapstärnigar", "6", [0, 0, 0, 0, 0, 1], "Vit", True)

number_of_base = input ("Select the number of dice to roll: ")

from dice import roll_dice
if number_of_base != "0":
    roll_dice(int(number_of_base), base)
    victory_points, skulls = roll_dice()
dice.py full file
import random
class Dice:
    def __init__(self, dice_type, sides, result_list, color, damage):
        self.dice_type= dice_type
        self.sides = sides
        self.result_list = result_list
        self.color = color
        self.damage = damage

def roll_dice(number_of_roll, base):
    victory_points = 0
    skulls = 0
    for x in range(int(number_of_roll)):
        dice_outcome = random.randint(1, int(base.sides))
        victory_points = victory_points + int(base.result_list[(dice_outcome)-1])
        if dice_outcome == 1 and base.damage is True:
           skulls = skulls + 1
    return victory_points, skulls
Same error as before:
Error:
Traceback (most recent call last): File "/Users/andreas.freij/PycharmProjects/Forbidden Lands Dice Generator/main.py", line 27, in <module> victory_points, skulls = roll_dice() TypeError: roll_dice() missing 2 required positional arguments: 'number_of_roll' and 'base'
Are you messing with me on purpose, or is it by accident? My previous post starts with:

Quote:The error you have is either because you are not providing the correct arguments when you call the function:
victory_points, skulls = roll_dice()
And here you are making exactly the same mistake. You call roll_dice() without any arguments. But when you look at the declaration for roll_dice() it is clear to see that you need to provide two arguments.
def roll_dice(number_of_roll, base)
The error message even states this VERY clearly.
Error:
TypeError: roll_dice() missing 2 required positional arguments: 'number_of_roll' and 'base'
Sorry deanhystad and others. Total beginner!
But I finally got what you where saying and it now works!

Thanks!