simbax

lista.py

Dec 29th, 2012
6,461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. # coding=utf-8
  2. # Program dopisuje do pliku "lista.txt" kolejnych studentów
  3. class Student:
  4.     imie = "Gal"
  5.     nazwisko = "Anonim"
  6.     grupa = 0
  7.  
  8. def NowiStudenci(ile_dotychczas):
  9.     lista = []
  10.     for x in range(ile_dotychczas):
  11.         lista.append(Student()) # "zerowanie" tablicy liczbą studentow, którzy są już na liście
  12.     nr = ile_dotychczas # zapisujemy sobie numer ostatniego studenta
  13.     while True:
  14.         s = raw_input("Podaj imię studenta nr %i (ENTER = koniec): " % (nr+1))
  15.         if not s: break
  16.         lista.append(Student())
  17.         lista[nr].imie = s
  18.         lista[nr].nazwisko = raw_input("Podaj nazwisko studenta nr %i: " % (nr+1))
  19.         lista[nr].grupa = input("Podaj numer grupy studenta nr %i: " % (nr+1))
  20.         nr += 1
  21.     return lista
  22.  
  23. print "Otwieram plik 'lista.txt'..."
  24. # próbujemy otworzyć plik
  25. plik = open("lista.txt", "a+r") # atrybut 'a' - dopisywanie do pliku
  26. # ten atrybut tworzy też plik, jeżeli on nie istnieje
  27. print "Plik 'lista.txt' otwarty poprawnie."
  28. # dopisanie kolejnych do listy
  29. lista_nowych = NowiStudenci(len(plik.readlines()))
  30. for nr, student in enumerate(lista_nowych):
  31.     if student.nazwisko == "Anonim": continue
  32.     plik.write("%03i. %s %s, grupa: %02i\n" % (nr+1, student.imie, student.nazwisko, student.grupa))
  33.     plik.flush()
  34.     print "Dopisano studenta..."
  35. plik.close()
Advertisement
Add Comment
Please, Sign In to add comment