Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import json
  2.  
  3. if __name__ == "__main__":
  4. money = int(input("เงินที่ได้รับประจำวัน: "))
  5. with (open(r"save.json","w+")) as file:
  6. choice = ""
  7. msg = {}
  8. msg['dairy'] = []
  9. isTrue = False
  10. total = 0.0
  11. while not isTrue:
  12. choice = input("\t\tMenu\n[A]dd [T]otal [E]xit: ").split() # splite / string to list / cut spacebar
  13. if(choice[0].upper() == 'A'):
  14. temp = {}
  15. temp["list"] = input("ชื่อรายการใช้จ่าย? ")
  16. temp["price"] = float(input("ค่าใช้จ่าย? "))
  17. msg['dairy'].append(temp)
  18. elif(choice[0].upper() == 'T'):
  19. file.write(json.dumps(msg,ensure_ascii=False,indent=4))
  20. file.seek(0,0)
  21. data = json.loads(file.read())
  22. print("\n\t\t\t\tสรุปค่าใช้จ่าย\n{:^28} {:^10} {}".format("รายการ","ราคา","รวม"))
  23. i=0
  24. for row in data["dairy"]:
  25. i+=1
  26. total += row["price"]
  27. print("{:>2}.{:<25} {:<10} ".format(i,row["list"],row["price"]))
  28. print("{:28} {:>18}".format("ค่าใช้จ่ายทั้งหมด",total))
  29. print("{:39} {}".format("เงินคงเคลือ", money-total))
  30. elif(choice[0].upper() == 'E'):
  31. file.close()
  32. isTrue = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement