jep jep - 4 months ago 5
Python Question

Returning multiple valuables and then calling those valuables separately

I'm having issue with allowing a function to call on variables set by another function. I believe I know how to do this with single variables, but my code requires it be done with multiple variables, a challenge I've struggled with for hours. I've read much about ways others seem to have done this but I can't find success in implementing them.

#gathers the user's requests
def ask():
userw = int(input('How wide? '))
userh = int(input('How tall? '))
userc = input('What string to use? ')
userc_len = int(len(userc))
return (userw, userh, userc, userc_len)

#draws the rows of the box. First the top with the topbot function, then the body with body(), then the bottom with topbot again
def draw(w, h, c, c_len):
def topbot(w_, c_):
for x in range(w_):
print (c_, end ='')
print ('\n')
def body(w_, h_, c_, c_len_):
for x in range(h_-2):
print (c_, end = '')
for x in range(w_-2):
print(' ' * c_len_, end = '')
print (c_)

topbot(w, c)
body(w, h, c, c_len)
topbot(w, c)

#begins draw
draw(userw, userh, userc, userc_len)

The problem begins when the
function tries to begin with the arguments of
userw, userh, userc, userc_len
, but can't find them:

NameError: name 'userw' is not defined

is returned when I try to run it.

  1. Is it correct to define
    within the
    function and manage the arguments how I did?

  2. How do I return the four variables from
    in a manner such that
    can then use them as arguments?


ask() is a function which will return 4 values. So,

returnValues = ask()
draw = draw(*returnValues) 
or simply, draw = draw(*ask())

Also, end = ' ' is not correct. Instead of that you can just use print(c_,''). Include Validations wherever necessary. Like what if I type "hi" for "How wide?". In this case the program should tell me that this is wrong.