2017-09-23 48 views
0
# -*- 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) 

我得到這個錯誤: 回溯(最近通話最後一個): 文件 「C:\用戶\索爾特\桌面\ python的ératségi\ötszáz\ otszaz.py」 23行在 kosar.append(dolog) NameError:名字「科薩」沒有定義python我得到一個nameerror?我已經有一個變量命名的

回答

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 = []

相關問題