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:
this triggers
PLEASE HELP
I wrote this up. it's a self contained script:
#!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') It works. Except for [input]b[/input]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 definedPerhaps someone could clarify foo() vs foo(bar) and their nuances in nested functions? Please?
