Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- # Program dopisuje do pliku "lista.txt" kolejnych studentów
- class Student:
- imie = "Gal"
- nazwisko = "Anonim"
- grupa = 0
- def NowiStudenci(ile_dotychczas):
- lista = []
- for x in range(ile_dotychczas):
- lista.append(Student()) # "zerowanie" tablicy liczbą studentow, którzy są już na liście
- nr = ile_dotychczas # zapisujemy sobie numer ostatniego studenta
- while True:
- s = raw_input("Podaj imię studenta nr %i (ENTER = koniec): " % (nr+1))
- if not s: break
- lista.append(Student())
- lista[nr].imie = s
- lista[nr].nazwisko = raw_input("Podaj nazwisko studenta nr %i: " % (nr+1))
- lista[nr].grupa = input("Podaj numer grupy studenta nr %i: " % (nr+1))
- nr += 1
- return lista
- print "Otwieram plik 'lista.txt'..."
- # próbujemy otworzyć plik
- plik = open("lista.txt", "a+r") # atrybut 'a' - dopisywanie do pliku
- # ten atrybut tworzy też plik, jeżeli on nie istnieje
- print "Plik 'lista.txt' otwarty poprawnie."
- # dopisanie kolejnych do listy
- lista_nowych = NowiStudenci(len(plik.readlines()))
- for nr, student in enumerate(lista_nowych):
- if student.nazwisko == "Anonim": continue
- plik.write("%03i. %s %s, grupa: %02i\n" % (nr+1, student.imie, student.nazwisko, student.grupa))
- plik.flush()
- print "Dopisano studenta..."
- plik.close()
Advertisement
Add Comment
Please, Sign In to add comment