Python, I have a questionDragon

Asked 1 years ago, Updated 1 years ago, 68 views

a=input() print(a['birth'])

In input brackets, {'birth' : '1234', 'name' : 'abc'} are all yours, is there any way to get an error? Please tell me why there is an error ㅠ<

python dictionary

2022-09-21 15:20

1 Answers

First, input() treats everything you enter as a string. Because the {'birth':'1900', 'name':'frank'} that you enter will be a string, you must replace it with a dictionary data type. You can then do the ast module , which changes the string to a dictionary data type.

import ast

a = "{'birth':'1900', 'name':'frank'}"
a_dic = ast.literal_eval(a)
print(a_dic['birth'])


2022-09-21 15:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.