Click here to Skip to main content
15,910,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add,update,delete data in json file. Therefore i am using if elif else statement for the put conditions, but in elif ladder a syntax error is shown at elif : expected expression. I am also using if with elif block for another condition for different function. How to solve this ?

What I have tried:

import json

print("Manage Employee Detail: ")
print("(a) for Add")
print("(u) for Update")
print("(d) for Delete")
Choice = (input("Enter Your choice: "))


if (Choice=="a") :
emp_dic =[{
"name" : input("Enter employee name : "),
"emp_id" : "2022-001",
"city" : input ("Enter city : "),
"experience" : int(input("Enter experience :")),
"ctc" : int(input("Enter CTC : ")),
"age" : int(input("Enter age : ")),
"contact" : int(input("Enter contact : "))
}]
with open ("emp.json","r") as file:
dic=json.load(file)

dic.append(emp_dic)

with open("emp.json","w") as file:
json.dump(dic, file , indent=4)
print(dic)

elif Choice=="u" :
print(dic)
enter_id = input("Enter emp_id you want to update: ")
jsonfile=open("emp.json","r")
data=json.load(jsonfile)
jsonfile.close()
if enter_id=="emp_id":
data["name"] = input("Enter employee name : ")
data["emp_id"] = "2022-001"
data["city"] = input ("Enter city : ")
data["experience"]= int(input("Enter experience :"))
data["ctc"] = int(input("Enter CTC : "))
data["age"] = int(input("Enter age : "))
data["contact"] = int(input("Enter contact : "))

jsonfile =open("emp.json", "w")
json.dump(data, jsonfile, indent=4)


elif Choice == "d":
def delete():
print(dic)
enter_id = input("Enter emp_id you want to delete: ")
jsonfile=open("emp.json","r")
data=json.load(jsonfile)
jsonfile.close()
if enter_id=="emp_id":
del data[enter_id]


else:
print("invalid input: ")
Posted
Updated 8-Jun-22 21:44pm

1 solution

Quote:
Why the elif condition throws expected expression error in Python?

The answer is an indentation problem!
In Python, indentation is part of the language structure.
Python
import json

print("Manage Employee Detail: ")
print("(a) for Add")
print("(u) for Update")
print("(d) for Delete")
Choice = (input("Enter Your choice: "))


if (Choice=="a") : # if starts here
  emp_dic =[{
      "name" : input("Enter employee name : "),
      "emp_id" : "2022-001",
      "city" : input ("Enter city : "),
      "experience" : int(input("Enter experience :")),
      "ctc" : int(input("Enter CTC : ")),
      "age" : int(input("Enter age : ")),
      "contact" : int(input("Enter contact : ")) 
      }]
# and if ends here because of next line
with open ("emp.json","r") as file: # indent this
     dic=json.load(file)

     dic.append(emp_dic)
 
with open("emp.json","w") as file: # indent this
    json.dump(dic, file , indent=4)
    print(dic)

elif Choice=="u" :

it gives
Python
if (Choice=="a") : 
  emp_dic =[{
      "name" : input("Enter employee name : "),
      "emp_id" : "2022-001",
      "city" : input ("Enter city : "),
      "experience" : int(input("Enter experience :")),
      "ctc" : int(input("Enter CTC : ")),
      "age" : int(input("Enter age : ")),
      "contact" : int(input("Enter contact : ")) 
      }]
  with open ("emp.json","r") as file:
     dic=json.load(file)

     dic.append(emp_dic)
 
  with open("emp.json","w") as file:
    json.dump(dic, file , indent=4)
    print(dic)

elif Choice=="u" :

...
 
Share this answer
 
Comments
CPallini 9-Jun-22 3:47am    
5.
Patrice T 9-Jun-22 4:05am    
Thank you.
Member 15667238 9-Jun-22 4:08am    
Thank you, Now it's working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900