'How do i use comprehensions to output dictionary data after editing it and how to validate user input asking user to repeat
eList = []
while True:
eDict = {
"ID": "",
"NAME": "",
"EMAIL": "",
"ADDRESS": "",
"SALARY": ""}
try:
eId = int(input("Enter Employee ID: "))
if len(str(eId)) <= 7:
pass
else:
print("ID should be of length less than or equal to 7")
except ValueError:
print(" ")
try:
eName = input("Enter Employee Name: ")
if len(eName) > 2:
eDict["NAME"] = eName
else:
print("Length haiskuita wangu")
except:
print("zvatoramba ka")
continue
try:
eEmail = input("Enter Employee Email: ")
if len(eEmail) > 2:
print("Done")
else:
print("Length haiskuita wangu")
except:
print("zvatoramba ka")
continue
try:
eAddress = input("Enter Employee Address: ")
if len(eAddress) > 2:
print("Done")
else:
print("Length haiskuita wangu")
except:
print("zvatoramba ka")
continue
try:
eSalary = float(input("Enter Employee Salary: "))
if 18 < eSalary > 27:
print("Done")
else:
print("Length haiskuita wangu")
except:
print("zvatoramba ka")
continue
eDict["ID"] = eId
eDict["NAME"] = eName
eDict["EMAIL"] = eEmail
eDict["ADDRESS"] = eAddress
eDict["SALARY"] = eSalary
eList.append(eDict)
if input("Do you want to continue? [y/n]: ") != "y":
break
eList[eDict["SALARY"]] = eSalary*1.3
eDict.update({"NAME": eName + " IT Department"})
print(eList)
This is a list of dictionaries and here i am trying to change each NAME after compiling all the data. With the try and except im trying to validate user information and make it tell the user to repeat again when they enter wrong information. Im not getting it right.
Solution 1:[1]
As @timus mentioned above, there should be a continue
statement for all the else
parts. You can also raise an exception from else since all the exceptions have continue block. You need to loop through eList
in order to update all the names.
I think the below code should do the trick.
eList = []
while True:
eDict = {
"ID": "",
"NAME": "",
"EMAIL": "",
"ADDRESS": "",
"SALARY": ""}
try:
eId = int(input("Enter Employee ID: "))
if len(str(eId)) <= 7:
pass
else:
print("ID should be of length less than or equal to 7")
raise ValueError
eName = input("Enter Employee Name: ")
if len(eName) > 2:
eDict["NAME"] = eName
else:
print("Length haiskuita wangu")
raise Exception
eEmail = input("Enter Employee Email: ")
if len(eEmail) > 2:
print("Done")
else:
print("Length haiskuita wangu")
raise Exception
eAddress = input("Enter Employee Address: ")
if len(eAddress) > 2:
print("Done")
else:
print("Length haiskuita wangu")
raise Exception
eSalary = float(input("Enter Employee Salary: "))
if 18 < eSalary > 27:
print("Done")
else:
print("Length haiskuita wangu")
raise Exception
except ValueError:
print(" ")
continue
except:
print("zvatoramba ka")
continue
eDict["ID"] = eId
eDict["NAME"] = eName
eDict["EMAIL"] = eEmail
eDict["ADDRESS"] = eAddress
eDict["SALARY"] = eSalary
eList.append(eDict)
if input("Do you want to continue? [y/n]: ") != "y":
break
for edict in eList:
edict["SALARY"] *= 1.3
edict.update({"NAME": edict["NAME"] + " IT Department"})
print(eList)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nishant |