Python Dosya İşlemleri Yeni Dosya Oluşturamıyor

Eiesenhower

Hectopat
Katılım
8 Nisan 2020
Mesajlar
821
Yer
İstanbul
Dosya işlemlerini kullanarak sınıftakilerin aldığı not ortalamasının harf notuna gelen karşılığını yapabildim ama bunları "Geçenler ve Kalanlar" adında iki yeni TXT dosyasına ayırmak istedim ama bir türlü yapamadım. Son iki "with open" bloğunda hatalar olduğunu biliyorum özellikle silmedim oradan tıkandım resmen. File 2 dosyasının çıktısı bu şekilde ama bundan AA ya da FF değerlerini alamadım bir türlü


Ahmet = AA
Ali = AA
Mehmet = AA
Serap = AA
Burak = AA
Sevim = AA
Sedat = AA
İlayda = AA
Memoş = FF
İlker = AA
Serpil = FF


Kod:
def not_hesapla(satir):
    liste=satir.split(",")
  
  
    isim=liste[0]
    not1=liste[1]
    not2=liste[2]
    not3=liste[3]
 
    nothesap= int(not1+not2+not3)/3
    if (nothesap>= 90):
  
        harf = "AA"
    elif (nothesap >= 85):
        harf = "BA"
    elif (nothesap >= 80):
        harf = "BB"
    elif (nothesap >= 75):
        harf = "CB"
    elif (nothesap >= 70):
        harf = "CC"
    elif (nothesap >= 65):
        harf = "DC"
    elif (nothesap >= 60):
        harf = "DD"
    elif (nothesap >= 55):
        harf = "FD"
    else:
        harf = "FF"
    return isim+" = "+ str(harf) +"\n"





with open("notlar.txt","r",encoding="utf-8") as file:
    liste2=[]
  
  
    for i in file:
        liste2.append(not_hesapla(i))
    with open("notlar1.txt","w",encoding="utf-8") as file2:
      
        for i in liste2:
          
            file2.write(i)
            file.close()
          
    with open("notlar1.txt","r",encoding="utf -8") as file2:
        liste3=[]
        for i in file2:
            liste3.append(i)
  
  
      
    with open("notlar2.txt","w",encoding="utf-8") as file3:
        for i in liste3:
            file3.write(i)
 
Son düzenleme:
string elemanların her birini int'e çevirmelisin, with kullanıyorsan file.close() çağırmana lüzum yok otomatik kendisi çağırır, dosyadan okurken verideki new line karakterlerini strip ile temizleyebilirsin ardından split(" = ") şeklinde name ve harf içeren bir liste döndürebilirsin.

Bu kadar okuma yazma yapmaya lüzum yok ancak ödevin böyle sanırım, gerisini sen halledersin.

Python:
def not_hesapla(satir):
    liste=satir.split(",")
 
 
    isim=liste[0]
    not1=int(liste[1])
    not2=int(liste[2])
    not3=int(liste[3])
 
    nothesap= (not1+not2+not3) /3

    if (nothesap>= 90):
 
        harf = "AA"
    elif (nothesap >= 85):
        harf = "BA"
    elif (nothesap >= 80):
        harf = "BB"
    elif (nothesap >= 75):
        harf = "CB"
    elif (nothesap >= 70):
        harf = "CC"
    elif (nothesap >= 65):
        harf = "DC"
    elif (nothesap >= 60):
        harf = "DD"
    elif (nothesap >= 55):
        harf = "FD"
    else:
        harf = "FF"
    return isim+" = "+ str(harf) +"\n"



with open("notlar.txt","r",encoding="utf-8") as file:
    notlar = []
    for i in file:
        notlar.append(not_hesapla(i))
    with open("notlar1.txt","w",encoding="utf-8") as file2:
        for j in notlar:
            file2.write(j)

with open("notlar1.txt","r",encoding="utf-8") as file:
    for i in file:
        student = i.strip().split(" = ")
        print("name:", student[0], "grade:", student[1])
 
string elemanların her birini int'e çevirmelisin, with kullanıyorsan file.close() çağırmana lüzum yok otomatik kendisi çağırır, dosyadan okurken verideki new line karakterlerini strip ile temizleyebilirsin ardından split(" = ") şeklinde name ve harf içeren bir liste döndürebilirsin.

Bu kadar okuma yazma yapmaya lüzum yok ancak ödevin böyle sanırım, gerisini sen halledersin.

Python:
def not_hesapla(satir):
    liste=satir.split(",")
 
 
    isim=liste[0]
    not1=int(liste[1])
    not2=int(liste[2])
    not3=int(liste[3])
 
    nothesap= (not1+not2+not3) /3

    if (nothesap>= 90):
 
        harf = "AA"
    elif (nothesap >= 85):
        harf = "BA"
    elif (nothesap >= 80):
        harf = "BB"
    elif (nothesap >= 75):
        harf = "CB"
    elif (nothesap >= 70):
        harf = "CC"
    elif (nothesap >= 65):
        harf = "DC"
    elif (nothesap >= 60):
        harf = "DD"
    elif (nothesap >= 55):
        harf = "FD"
    else:
        harf = "FF"
    return isim+" = "+ str(harf) +"\n"



with open("notlar.txt","r",encoding="utf-8") as file:
    notlar = []
    for i in file:
        notlar.append(not_hesapla(i))
    with open("notlar1.txt","w",encoding="utf-8") as file2:
        for j in notlar:
            file2.write(j)

with open("notlar1.txt","r",encoding="utf-8") as file:
    for i in file:
        student = i.strip().split(" = ")
        print("name:", student[0], "grade:", student[1])
Her satırım hata dolu nerdeyse... Çok teşekkür ederim hocam.
 

Geri
Yukarı