V
V
Vlad Avramenko2021-08-07 16:36:28
Python
Vlad Avramenko, 2021-08-07 16:36:28

I don't understand why the error?

I did not understand how to display with indents

import json


C:\Users\Vlad\Desktop>"C:\Users\Vlad\Desktop\Python File (3).py"
Traceback (most recent call last):
File "C:\Users\Vlad\Desktop\Python File (3).py", line 55, in
func()
File "C:\Users\Vlad\Desktop\Python File (3).py", line 9, in func
contacts = json.load(f)
File "D :\install Python\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
io.UnsupportedOperation: not readable


def func():

contacts = {"Petya":"BMW", "Lena":"AUDI","Andryukha":"Mazda"}

filename="C:/Users/Vlad/Desktop/contacts.json"
with open(filename,'a') as f:
contacts = json.load(f)

while True:
command = input("\nSelect command\n1 - Add car\n2 - Remove car\n3 - View car list\nEnter command: ")
print()
if command == '1':
name = input("Owner name: " )
name = name.title()
car = input("Car brand: ")
if contacts.get(name):
print("Car is already in the parking lot")
continue
contacts[name] = car


with open(filename, "a" ) as f:
json.dump(contacts,f)
print("Data was saved")

elif command == "2":
name = input("Owner Name: ")
name = name.title()
if contacts.get( name):
contacts.pop(name)
print(f"{name} deleted")
else:
print("No such driver exists")


with open(filename, "a") as f:
json.dump(contacts,f)
print("Data has been saved")

elif command == "3":
for name, car in contacts.items():
print(f"{name}-{car}")

else:
print("There is no such command")

if __name__ == "__main__":
func()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-08-07
@krolik11zip

Invalid json means if the mode rhasn't changed anything. What does https://jsonformatter.curiousconcept.com/
show ?

H
HemulGM, 2021-08-07
@HemulGM

Because you don't know the language at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question