gpt4 book ai didi

python - 如何在字典中存储多个值?

转载 作者:行者123 更新时间:2023-12-03 23:26:33 26 4
gpt4 key购买 nike

如何在字典中存储多个值?

最佳答案

看起来你期待 stud_data要成为字典列表而不是字典,因此将其设为列表并使用 .append而不是 .update .
还:

  • 如果您实际上缩进以允许用户多次输入,则需要改变一些东西。
  • 你不需要flag .使用 while Truebreak需要的时候
  • 单个值不需要括号
  • stud_data = []
    while True:
    Name = input("Enter the student name :")
    Age = input("Enter the age :")
    Gender = input("Enter the {} grade :")
    repeat = input("Do you want to add input more?: ")
    stud_data.append({
    "Name": Name,
    "Age": Age,
    "Gender": Gender
    })
    if repeat == "no" or repeat == "NO":
    break

    print(stud_data)

    关于python - 如何在字典中存储多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64090334/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com