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