python - How to add values to a dictionary of class in python
问题描述
It's my first time posting here. I'm currently facing a problem where my goal is for user to have a command where my dictionary of classes will add exp to the class argument. (I'm kinda bad at english if you want some clarification, Please let me know)
class Stats:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
stre = {1: Stats(name='Excercise', exp=0, description="This is a description"),
2: Stats(name='Gym', exp=0, description="Gym description")}
def statsComm():
command_list = ["stre", "inte", "will"]
print("Select Command {}".format(command_list))
command = input("> ")
if command == 'stre':
for key, value in stre.items():
print(value.exp)
while True:
print("Enter Command {}".format(command_list))
command = input("> ")
if command == 'stats':
statsComm()
Currently it prints the right thing. But I don't know how to add value to exp of an individual class when they enter a command. Thank you :D
解决方案
You can specify individual classes within the for loop as follows:
for key, value in stre.items():
if value.name == 'Exercise':
value.exp += 1
推荐阅读
- javascript - Pressable 和 TouchableOpacity 的区别
- pandas - 大熊猫的分组总和
- node.js - 未显示 Azure Web 应用程序/虚拟目录上的节点应用程序
- if-statement - 在eclipse中反转if语句
- java - 没有为 log4j2 的 Appender AchLog 问题提供名称属性
- python - Google Cloud PubSub 在异步拉取消息时抛出 504 Deadline Exceeded 错误
- python - 使用 Python2 与 Python3 进行 Base64 URL 解码
- css - 滚动捕捉类型接近使用哪个捕捉点?
- c++ - 尝试将 lambda 函数放入队列时出现一般分配器错误(可能与 unique_ptr 相关)
- azure-cosmosdb - azure cosmos数据库资源token有效期在哪里配置?