Feb-24-2018, 06:03 PM
I feel I'm shaky on understanding the interplay between passing variables through functions (outside of classes)...Basically IDK when to use foo() vs foo(bar) especially in freestanding (OUTSIDE of a class) nested functions.
PLEASE HELP
I wrote this up. it's a self contained script:
It works. Except for [input]b[/input]
this triggers



PLEASE HELP
I wrote this up. it's a self contained script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#!usr/bin/env python3 #defexp0.py import sys from functools import partial as p def inquire(a, type ,errmsg): while True : b = input (a) if b ! = '': try : b = = type (b) except ValueError: print ( '{}' . format (errmsg)) continue else : print ( 'ok' ) return b else : print ( f '{a:^50} must be answered.' ) continue the_q = p(inquire, type = str ,errmsg = 'sorry' ) def foo(): while True : quit_now = the_q( '\'y\' to quit anything else to continue' ) quit_now.strip().lower() if quit_now = = 'y' or quit_now = = 'yes' : sys.exit() else : bar() continue def bar(): quest = the_q( 'please enter a, b, c, d or etc.' ) quest.strip().lower() if quest = = 'a' : forA(quest) elif quest = = 'b' : forB() #won't work elif quest = = 'c' : forC(quest) elif quest = = 'd' : forD() elif quest = = 'e' : forE(quest) else : forEtc() def forA(x): print ( f '{x:^10}is fine.' ) def forB(): global someVariable print ( 'nothing...' . format (someVariable)) '''but this WOULD work: def forB(): global someVariable print('nothing...') I don't understand ''' def forC(y): print ( '{} works too...' . format (y)) def forD(): print ( 'fine' ) def forE(someVariable): global quest print ( 'should work...' . format (someVariable)) #forE() global variable names must be different from parameter names??? def forEtc(): print ( 'works' ) if __name__ = = '__main__' : foo() else : print ( 'sorry {__name__} not main' ) |
this triggers
Error:Traceback (most recent call last):
File "defexp0.py", line 77, in <module>
foo()
File "defexp0.py", line 30, in foo
bar()
File "defexp0.py", line 39, in bar
forB()#won't work
File "defexp0.py", line 54, in forB
print('nothing...'.format(someVariable))
NameError: name 'someVariable' is not defined
Perhaps someone could clarify foo() vs foo(bar) and their nuances in nested functions? Please?