简体   繁体   English

在Python中存储用户数据

[英]Storing user data in Python

I am trying to find a way to store data based on a user's move (input). 我试图找到一种基于用户的移动(输入)存储数据的方法。

For example, they can pick up a ball multiple times, and that gets added to their inventory (data storage). 例如,他们可以多次捡起一个球,然后将其添加到他们的库存(数据存储)中。

Any ideas as to how I could do this and be able to update the data, based on how many times it is picked up? 关于我如何做到这一点以及是否能够根据获取数据的次数来更新数据的任何想法?

Thanks 谢谢

Not enough details were provided, and therefore am giving you the best answer possible after interpreting your question. 没有提供足够的详细信息,因此在解释您的问题后给您最佳的答案。

You can use a dictionary to store all the items as the key, and the number of times the user picked that item up as a value. 您可以使用字典将所有项目存储为键,并将用户选择该项目的次数存储为一个值。

data = {"ball": 0, "bat": 0, "helmet":0}

answer = input("What would you like to pick up? (ball, bat, or a helmet?)")
if answer in data:
    data[answer] += 1
else:
    data[answer] = 1        

This was a simple example, and I had pre-popualated the dictionary. 这是一个简单的例子,我已经把字典预先放好了。 In your program you will have to check if the item exists or not, and then increment like I did. 在您的程序中,您将必须检查该项目是否存在,然后像我一样增加。 Hope this helped. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM