Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # ohjelma tasaa kulut, joita tekstitiedostossa on annettu
  2. def totalcalc(dict):
  3.     summa = 0
  4.     for i in dict:
  5.         summa = summa + float(dict[i])
  6.     return summa
  7.  
  8. def main():
  9.     tiedosto = input("Enter the name of the file: ")
  10.     lahtotied = open(tiedosto, "r")
  11.     dict = {}
  12.     testi = {}
  13.     for rivi in lahtotied:
  14.         test = rivi.strip()
  15.         name, sum = test.split(":")
  16.  
  17.         if name not in dict:
  18.             dict[name] = sum
  19.         elif name in dict:
  20.             dict[name] = float(dict[name]) + float(sum)
  21.     people = len(dict)
  22.     total = totalcalc(dict)
  23.     keskiarvo = total/people
  24.     print("Total costs: {:.2f}e".format(total))
  25.     print()
  26.     for i in dict:
  27.         dict[i] = float(dict[i]) - keskiarvo
  28.     for i in dict:
  29.         if dict[i] < -0.05:
  30.             t = dict[i] * -1
  31.             print(i + " has paid {:.2f} in the following amounts: ".format(t))
  32.             print(i + " needs to pay {:.2f}e.".format(t))
  33.             print()
  34.         elif dict[i] > 0.05:
  35.             print(i + " has paid {:.2f} in the following amounts: ".format(dict[i]))
  36.             print(i + " needs to receive {:.2f}e.".format(dict[i]))
  37.             print("")
  38.         else:
  39.             print(i + " has paid {:.2f} in the following amounts: ".format(dict[i]))
  40.             print("No transfers needed.")
  41.             print()
  42.     print(testi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement