python - How can I pass input to a function, then have it use that input across 5 internal functions that are chaining each other? -
this 2 questions think.
- how make internal functions use same raw_input?
- how chain functions input?
i've been trying make make circle shape whatever character user inputs. (this "for fun" activity thought me used functions. i've been self teaching python 2 weeks now)
far code is:
def circle(symbol): def lines1(aa): print(symbol * 20) aa() print(symbol * 20) return(lines1) def lines2(bb): print(symbol * 7 + ' ' * 6 + symbol * 7) bb() print(symbol * 7 + ' ' * 6 + symbol * 7) return(lines2) def lines3(cc): print(symbol * 4 + ' ' * 12 + symbol * 4) cc() print(symbol * 4 + ' ' * 12 + symbol * 4) return(lines3) def lines4(dd): print(symbol * 2 + ' ' * 16 + symbol * 2) dd() print(symbol * 2 + ' ' * 16 + symbol * 2) return(lines4) def lines5(): print(symbol + ' ' * 18 + symbol) print(symbol + ' ' * 18 + symbol) return(lines5) lines1(lines2(lines3(lines4(lines5())))) circle(raw_input()) example: if user inputs #, supposed output:
#################### ####### ####### #### #### ## ## # # # # ## ## #### #### ####### ####### #################### the problem doesn't output anything.
your cicles function returns early:
def circle(symbol): def lines1(aa): print(symbol * 20) aa() print(symbol * 20) return(lines1) the rest of function not executed.
next, use functions want call other functions, never pass in arguments. aa() not given reference lines2() function.
instead, call lines5(), returns none, pass lines4(), cannot call lines4().
you'll need inner wrappers make work way want to:
def circle(symbol): def lines1(inner): def wrapper(): print(symbol * 20) inner() print(symbol * 20) return wrapper def lines2(inner): def wrapper(): print(symbol * 7 + ' ' * 6 + symbol * 7) inner() print(symbol * 7 + ' ' * 6 + symbol * 7) return wrapper def lines3(inner): def wrapper(): print(symbol * 4 + ' ' * 12 + symbol * 4) inner() print(symbol * 4 + ' ' * 12 + symbol * 4) return wrapper def lines4(inner): def wrapper(): print(symbol * 2 + ' ' * 16 + symbol * 2) inner() print(symbol * 2 + ' ' * 16 + symbol * 2) return wrapper def lines5(): print(symbol + ' ' * 18 + symbol) print(symbol + ' ' * 18 + symbol) lines1(lines2(lines3(lines4(lines5))))() now functions lines1 through lines4 each return wrapper function passed next function, making them decorators. start lines5 (as function reference, not calling it call result of nested wrappers.
the definition of lines5 use @decorator syntax:
@lines1 @lines2 @lines3 @lines4 def lines5(): print(symbol + ' ' * 18 + symbol) print(symbol + ' ' * 18 + symbol) line5()
Comments
Post a Comment