How can data input be stored in a dictionary using Python?

In Python, you can use the input() function to get user input data and store it using key-value pairs in a dictionary. Here is an example code:

# 创建一个空字典
data = {}

# 获取用户输入
key = input("请输入键:")
value = input("请输入值:")

# 将用户输入的数据存入字典
data[key] = value

# 输出字典
print(data)

In this example, an empty dictionary called data is first created. The input() function is then used to get the user’s input for both the key and value and store them in the dictionary as key-value pairs. Finally, the contents of the dictionary are outputted.

Please note that both the keys and values entered by the user are of string type. If you need to convert the value to another type (such as an integer or a floating-point number), you can use the appropriate type conversion functions, such as int() or float().

bannerAds