简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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