Python Forum
Multiple return from function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple return from function
#1
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!
buran write Mar-22-2021, 05:29 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
try

result = roll_dice(int(number_of_Base), Base.dice_type, Base.sides,Base.result_list, Base.damage)
Reply
#3
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.
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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'
Reply
#7
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'
Reply
#8
Sorry deanhystad and others. Total beginner!
But I finally got what you where saying and it now works!

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,372 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Lambda function not return value mbilalshafiq 4 3,324 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,230 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,723 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,357 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,933 Jan-28-2020, 06:20 PM
Last Post: snippsat
  return outside function seamus 4 3,065 May-17-2019, 07:38 PM
Last Post: seamus
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,236 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Return result from multiple iteration condition vestkok 3 2,798 Sep-11-2018, 03:48 PM
Last Post: ichabod801
  Need of return in function if statement inside the function already returns Athul 5 3,922 Aug-16-2018, 10:19 AM
Last Post: DuaneJack

Forum Jump:

User Panel Messages

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