# -*- coding:utf-8 -*-
# ötszaz.py
# , 2017
érték = {"1":500,
"2":450,
"3":400}
# --- 1.feladat ---
with open("penztar.txt","r") as ff:
adatok = ff.read()
# --- 2. feladat ---
print("2.feladat\nA fizetések száma: {}".format(adatok.count("F")))
# --- 3.feladat ---
kosar = []
sok_kosar = []
for dolog in adatok.splitlines():
if dolog != "F":
kosar.append(dolog)
elif dolog == "F":
sok_kosar.append(kosar)
del kosar
##kosarak = elso(adatok.splitlines())
for i in sok_kosar: print(i)
我收到此错误: Traceback(最近一次调用最后一次): 文件“C:\ Users \ Zsolt \ Desktop \pythonératségi\ötszáz\ otszaz.py”,第23行,in kosar.append(dolog) NameError:名称'kosar'未定义
答案 0 :(得分:0)
for dolog in adatok.splitlines():
if dolog != "F":
kosar.append(dolog)
elif dolog == "F":
sok_kosar.append(kosar)
del kosar
在dolog == "F"
的迭代之后,您希望在迭代中发生什么?如果dolog == "F"
您删除了kosar
。在下一次迭代中,kosar
未定义。
您的代码基本上等同于:
>>> li = []
>>> nums = [1, 2, 3]
>>> for num in nums:
... print(num)
... if num == 2:
... del li
... else:
... li.append(num)
...
1
2
3
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
NameError: name 'li' is not defined
如果您要清空列表,而不是del kosar
使用kosar = []
。