I am a beginner in python and am doing an exercise that asks me to create a simple database in a .txt file that allows the user to register and query products and their values in reais, without having to access the file (open , but does not show on screen). Here is my code:
import os
try:
op = open('C:/Users/Daniel/Desktop/database.txt','a')
except:
op = open('C:/Users/Daniel/Desktop/database.txt', 'w')
dict = {}
while True:
menu = int(input("Type 1 to register,2 to consult or 3 to remove: "))
if menu == 1:
#cadastrar produtos
prod_cad = str(input("Type the product name: ")
if [dict.has_key(prod_cad)]:
print("This product is already registered.")
else:
val = float(input(" Type the price of product: "))
op.write(dict[prod_cad]=val",")
My question is precisely in this last part that records the name and value of the product in the file. I have no idea how to send these values to the file and organize so that each tuple 'product': value (int) is separated by commas and kept accessible for later user query. I know I can not write like this:%% cos_de% because I would be sending the name of the variable and not the value. Thanks in advance for any help!