-1

i'am learning python so please don't be too hard if my question seems simple to you.this is not the real program it's just to make it simple and clear for the question. the real program is supposed to create and save a dictionnary to a text file, and different functions to read, delete , add data to this dictionnary. i have to files for a program: 1 all my functions 2 the main prog

#file1
def create_dict():
    dico={}
    name=input('name')
    age=input('age')
    coef=input('coeff')
    dico[name]=(age,coef)
    return dico

def print_dict(dico):
    print(dico)

def fin():
    exit()

so i'm using a dict to then execute the appropriate function chosen.

#file2 main

from file1 import *
do={'C':create_dict,'P':print_dict,'E':fin} #dict of references to functions

while 1:        
    rep=input('choice?\n'    # ask user to chose what action 
              'C-reate dict\n'
              'P-rint dict\n'
              'E-xit\n')

    do[rep]() # execute the function chosen

So my questions are: how do i get the return of my first function create_dict? how to give the parameter needed for my function print_dict? thanks for your help

eric paris
  • 61
  • 3
  • 11

3 Answers3

0

I think you better redesign the functions, such that these take as input the dictionary, like:

#file1
def create_dict(dico):
    name=input('name')
    age=input('age')
    coef=input('coeff')
    dico[name]=(age,coef)
    return dico

def print_dict(dico):
    print(dico)
    return dico

def fin(dico):
    exit()

So now all functions have a uniform structure: taking a dictionary as input, and returning one.

Now we can rewrite the program to:

#file2 main
from file1 import create_dict, print_dict, fin

do={'C':create_dict,'P':print_dict,'E':fin} #dict of references to functions
dico = {}

while True:        
    rep=input('choice?\n'    # ask user to chose what action 
              'C-reate dict\n'
              'P-rint dict\n'
              'E-xit\n')

    dico = do[rep](dico) # execute the function chosen

So we initialize a variable dico with dico = {} and each time pass it to the function. In case the function does not need the parameter, that is not a problem, but it should return the dictionary (or a new one, in case we decide to create a new one).

You also better do not use star imports (like from file1 import *, since this is usually considered an anti-pattern as well). Yes occasionally it is useful, but it can do more harm than good.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • thanks a lot seems a good way to proceed. So if all functions have not the same structure it's not possible to give a parameter just for some? – eric paris Jan 31 '18 at 10:53
0

A way better approach would be to re-design your logic, but if that's off the table then...


You can save the result of your last action and then pass it on to the next:

last_result = []
while 1:
    rep = # get input action
    new_result = do[rep](*last_result)
    last_result = list(new_result) if new_result is not None else []

You'll have to update your action methods to take *args so as to avoid invocation errors (in case the action does not take any argument):

def create_dict(*args):
    # You don't have to use *args at all
    # ...

def print_dict(dico, *args):
    # ...

def fin(*args):
    # ...
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

You can get the return value of create_dict by assigning it to a variable.

a_ = create_dict()

When code above is ran, the variable a_ holds the dictionary value returned by the function.

Since the variable a_ holds the dictionary value returned by create_dict(), you can just give the function print_dict, a_ for the parameter like this.

print_dict(a_)

This would print out the entire dictionary in a line.

Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38