PyDLR31

# UnboundLocalError: local variable 'D' referenced before assignment
# Python Introductory Course 8.-10. April 2019
# Python 3.7

D = {}

def foo():
    if False:
        D = {}
    D["blue"] = "blau"
    print("D local:", D)
    return D


D = foo()
print(D)
::

# kuerzer: # 1. Line '*' commented out: NameError: name 'D' is not defined # 2. With line '*': UnboundLocalError: local variable 'D' referenced before assignment # 3. With 'if True': works def g():

System Message: ERROR/3 (<string>, line 31)

Unexpected indentation.
if False:
D = {} # (*)

System Message: WARNING/2 (<string>, line 33)

Definition list ends without a blank line; unexpected unindent.

print(D)

g()

PyDLR23

Switch/Case with Python

def a():
    print("'a' was called")

def b():
    print("'b' was called")

def c():
    print("'c' was called")


D = { 'a' : a, 'b' : b, 'c' : c}

while True:
    s = input("your choice: ")
    if s in D:
        D[s]()
    else:
        break

PyDLR22

Python 3

SciPy

Pandas

PyDLR (zuletzt geƤndert am 2019-04-11 19:17:44 durch HubertHoegl)