Thank you for browsing. Please forgive English grammar. Please kindly tell the experts if there is something you can do about it.
-Search for a key from the dictionary. -If the specified key is found, the value is output, and if it is not found, "The key was not found" is output. -When the above process is completed, select whether to continue, edit the dictionary, or end.
name_age = {"tanaka":33,"satou":23,"suzuki":29}
def dict_info (dict_tbl,key):
try:
print(key,"'s",dict_tbl[key]," years old.")
except LookupError:
print("[Key is not found.]")
op = 0
while op !=1:
if op == 0:
key = input("[Enter a name of the target.]:")
dict_info(name_age,key)
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
if op == 2:
print("[Edit the dictionary]")
addn = input("[EDIT][Enter a name of the target]:")
adda = int(input("[EDIT][Enter a age of the target.]:"))
name_age[addn] = adda
op = int(input("[Choose 0 or 1 or 2]:"))
if op !=1 and op !=0 and op !=2:
while op != 0 and op != 1 and op != 2:
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
[Enter a name of the target.]:niwa
[Key is not found.]
[Conitnue>0]
[End>1]
[Edit the dictionary>2]
[Choose 0 or 1 or 2]:0
[Enter a name of the target.]:tanaka
tanaka 's 33 years old.
[Conitnue>0]
[End>1]
[Edit the dictionary>2]
[Choose 0 or 1 or 2]:2
[Edit the dictionary]
[EDIT][Enter a name of the target]:masato
[EDIT][Enter a age of the target.]:19
{'tanaka': 33, 'satou': 23, 'suzuki': 29, 'masato': 19}
[Choose 0 or 1 or 2]:1
The name of the person is entered in the key of the dictionary name_age, and the age is entered in the value.
name_age = {"tanaka":33,"satou":23,"suzuki":29}
If the key is found by try, print it. If the key is not found with except, "Key is not found." Is displayed.
def dict_info (dict_tbl,key):
try:
print(key,"'s",dict_tbl[key]," years old.")
except LookupError:
print("[Key is not found.]")
Specify the key with input. op will be used later in while.
key = input("[Enter a name of the target.]:")
dict_info(name_age,key)
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
addn specifies the key (personal name) to add. adda specifies the value (age) to add.
print("[Edit the dictionary]")
addn = input("[EDIT][Enter a name of the target]:")
adda = int(input("[EDIT][Enter a age of the target.]:"))
name_age[addn] = adda
print(name_age)
op = int(input("[Choose 0 or 1 or 2]:"))
Two sentences, one for continuing and one for editing the dictionary, are inserted as shown below. I don't want to use brake, which is a forced termination as much as possible, so when I enter 1 for termination, the while loop ends. In the last if, if the wrong option is selected, it loops with while until the correct option is selected.
while op !=1:
if op == 0:
key = input("[Enter a name of the target.]:")
dict_info(name_age,key)
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
if op == 2:
print("[Edit the dictionary]")
addn = input("[EDIT][Enter a name of the target]:")
adda = int(input("[EDIT][Enter a age of the target.]:"))
name_age[addn] = adda
print(name_age)
op = int(input("[Choose 0 or 1 or 2]:"))
if op !=1 and op !=0 and op !=2:
while op != 0 and op != 1 and op != 2:
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
Completed by combining all.
name_age = {"tanaka":33,"satou":23,"suzuki":29}
op = 0
def dict_info (dict_tbl,key):
try:
print(key,"'s",dict_tbl[key]," years old.")
except LookupError:
print("[Key is not found.]")
while op !=1:
if op == 0:
key = input("[Enter a name of the target.]:")
dict_info(name_age,key)
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
if op == 2:
print("[Edit the dictionary]")
addn = input("[EDIT][Enter a name of the target]:")
adda = int(input("[EDIT][Enter a age of the target.]:"))
name_age[addn] = adda
print(name_age)
op = int(input("[Choose 0 or 1 or 2]:"))
if op !=1 and op !=0 and op !=2:
while op != 0 and op != 1 and op != 2:
print("[Conitnue>0]","\n","[End>1]","\n","[Edit the dictionary>2]")
op = int(input("[Choose 0 or 1 or 2]:"))
Before posting, I thought, "I think you can only add it while calling it a dictionary edit." I don't call myself from the beginning to the end, so I want to call myself in the future. English is cool, but it's annoying to see. I wrote it at the beginning, but since I'm a beginner, I think it's strange, but please forgive me.
I want to quit beginners soon.
Recommended Posts