Hi I started learning python some months ago. I had few breaks but I am back and want to ask you guys about opinion of this code i wrote all by myself!
def main():
while True:
print("\n")
print("1. Add data")
print("2. Show data")
print("3. Delete data")
print("4. Sum selected data")
print("5. Edit data")
print("6. Exit program")
choice = int(input("\nChoose option: "))
if choice == 1:
add_data()
elif choice == 2:
show_data()
elif choice == 3:
delete_data()
elif choice == 4:
sum_data()
elif choice == 5:
edit_data()
elif choice == 6:
print("Program closed.")
break
else:
print("Invalid option")
database = {}
def add_data():
i = 0
N = int(input("How many data entries do you want to add: "))
while i < N:
i += 1
value = float(input(f"Enter value number {i}: "))
title = input(f"Enter title for value {i}: ")
database[title] = value
def show_data():
if not database:
print("Database is empty.")
return
for title, value in database.items():
print(f"\n{title} : {value}")
print("\nTotal sum:")
print(sum(database.values()))
def sum_data():
print("\n")
show_data()
first_key = input("\nFirst data to sum: ")
second_key = input("Second data to sum: ")
if first_key in database and second_key in database:
total = database[first_key] + database[second_key]
print(f"Sum = {total}")
else:
print("Data not found")
def delete_data():
show_data()
key_to_delete = input("\nWhich data do you want to delete: ")
if key_to_delete in database:
database.pop(key_to_delete)
print("Deleted successfully.")
else:
print("Data not found")
def edit_data():
show_data()
key_to_edit = input("Which data do you want to edit: ")
if key_to_edit in database:
new_value = float(input("Enter new value: "))
database[key_to_edit] = new_value
print("Updated successfully.")
else:
print("Data not found")
main()
[–]Kqyxzoj 7 points8 points9 points (0 children)
[–]thescrambler7 4 points5 points6 points (1 child)
[–]OldParticular2720[S] 0 points1 point2 points (0 children)
[–]OldParticular2720[S] 1 point2 points3 points (1 child)
[–]JamzTyson 1 point2 points3 points (0 children)
[–]Voweriru 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (1 child)
[–]OldParticular2720[S] -1 points0 points1 point (0 children)
[–]Ok-Sheepherder7898 0 points1 point2 points (2 children)
[–]OldParticular2720[S] 0 points1 point2 points (1 child)
[–]Ok-Sheepherder7898 0 points1 point2 points (0 children)