Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- def clear():
- print("\n"*47)
- def json_print(json_filename):
- with open(json_filename, "r") as json_name:
- for i, j in dict(json.load(json_name)).items():
- print(f"{i}: ")
- print(f"\tNumber of copies: {j[0]}")
- print(f"\tPrice: {j[1]}")
- def json_add(json_filename):
- with open(json_filename, "r") as json_name:
- tmp_dict = dict(json.load(json_name))
- new_magazine_name = input("Enter magazine name: ")
- tmp = tmp_dict.get(new_magazine_name, -1)
- while tmp != -1:
- print("ERROR: this name was used.")
- print("Please, try again")
- new_magazine_name = input("Enter magazine name: ")
- tmp = tmp_dict.get(new_magazine_name, -1)
- new_number_of_copies = int(input("Enter number of copies magazine: "))
- new_price = int(input("Enter price: "))
- tmp_dict.update({new_magazine_name: (new_number_of_copies, new_price)})
- with open(json_filename, "w") as json_name:
- json.dump(tmp_dict, json_name)
- def json_del_elem(json_filename):
- with open(json_filename, "r") as json_name:
- tmp_dict = dict(json.load(json_name))
- del_name = input("Enter name you want to delete: ")
- tmp = tmp_dict.get(del_name, -1)
- while tmp == -1:
- print("ERROR: this name not found.")
- print("Please, try again")
- del_name = input("Enter name you want to delete: ")
- tmp = tmp_dict.get(del_name, -1)
- tmp_dict.pop(del_name)
- with open(json_filename, "w") as json_name:
- json.dump(tmp_dict, json_name)
- def json_search(json_filename):
- with open(json_filename, "r") as json_name:
- tmp_dict = dict(json.load(json_name))
- del_name = input("Enter name you want to search: ")
- tmp = tmp_dict.get(del_name, -1)
- if tmp == -1:
- print("This name not found.")
- else:
- print(f"\tNumber of copies: {tmp[0]}")
- print(f"\tPrice: {tmp[1]}")
- return tmp
- def json_calc(json_filename):
- with open(json_filename, "r") as json_name:
- tmp_dict = dict(json.load(json_name))
- res = 0
- count = 0
- for i, j in tmp_dict.items():
- if j[0] < 10000:
- count += 1
- res += j[1]
- res /= count
- return res
- def menuPrint():
- clear()
- print("1 - Show All element")
- print("2 - Search element")
- print("3 - Delete element")
- print("4 - Add new element or updates")
- print("5 - Average price of magazines with a circulation of less than 10000")
- print("0 - Exit")
- pressKey = input("Enter a selection -> ")
- clear()
- return pressKey
- def json_menu(json_filename):
- pressKey = menuPrint()
- if pressKey == '1':
- json_print(file_name)
- elif pressKey == '2':
- json_search(json_filename)
- elif pressKey == '3':
- json_del_elem(file_name)
- json_print(file_name)
- elif pressKey == '4':
- json_add(file_name)
- json_print(file_name)
- elif pressKey == '5':
- average_price = json_calc(file_name)
- print(f"Average price of magazines with a circulation of less than 10000: {average_price}")
- elif pressKey == '0':
- exit(0)
- else:
- print("ERROR: menu under this number does not exist.")
- input("Enter any key to continue -> ")
- json_menu(json_filename)
- name_magazine = ("eqwdfszx", "bgfdvscz", "bvvtgf", "sbtygfdfvsd", "asdf")
- number_of_copies = [8000, 14000, 20000, 9000, 16000]
- price = [1100, 1000, 1200, 500, 1300]
- magazine_dict = {k: v for k, v in zip(name_magazine, {k: v for k, v in zip(number_of_copies, price)}.items())}
- file_name = "file_name.json"
- with open(file_name, "w") as write_json:
- json.dump(magazine_dict, write_json)
- json_menu(file_name)
Add Comment
Please, Sign In to add comment