[Tutor] (no subject)
Roelof Wobben
rwobben at hotmail.com
Sun Sep 5 15:44:09 CEST 2010
Hello,
I have made this program as solution to a exercise from thinking like a computer scientist.
def encapsulate(val, seq):
if type(seq) == type(""):
return str(val)
if type(seq) == type([]):
return [val]
return (val,)
def insert_in_middle(val, seq):
middle = len(seq)/2
return seq[:middle] + encapsulate(val, seq) + seq[middle:]
def make_empty(seq):
"""
>>> make_empty([1, 2, 3, 4])
[]
>>> make_empty(('a', 'b', 'c'))
()
>>> make_empty("No, not me!")
''
"""
if type(element) == type([]):
for word2 in seq :
word2 = ""
elif type(element) == type(()):
tup2 = list (seq)
while teller > tup2.len():
tup2[teller]=""
teller = teller + 1
seq = tuple(tup2)
else:
seq = ""
test = make_empty([1, 2, 3, 4])
print test
But now Im getting this error message :
Traceback (most recent call last):
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 33, in <module>
test = make_empty([1, 2, 3, 4])
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 21, in make_empty
if type(element) == type([]):
NameError: global name 'element' is not defined
What went wrong here ?
Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100905/d978fb45/attachment.html>
More information about the Tutor
mailing list