In today's lab, we will be diving deeper into more Python data structures with lists and dictionaries.
Make a new script called shopping_list.py
Declare an empty list called “cart”.
Make another list called “items” and fill it with dictionaries holding item data.
Item data could include name, price, type, flavor, or anything relevant to your kind of store.
Create a function that appends chosen items from the items list to the cart.
Print out the cart each time an item is added, along with a subtotal, or other information you want to display.
Add a main loop to keep the script alive.
Update an existing game you have built to include data structure like lists and/or dictionaries. You can also use the time to add more elements, enhance the game, or change any gameplay elements. For example:
Make more reusable code
Change the speed of the gameplay, if relevant.
Add more animations and user feedback.
Store high scores in a .txt file. (you will have to figure out how!) This might help.
Add any new gameplay elements you want.
Lists are one of the four built-in data types in Python. They are used to store multiple items in a single variable.
myHobbies = ["drawing", "coding", "cooking"]
Lists use brackets around all of the items, with commas separating each item. The items are ordered and can be duplicated. within the list
Dictionaries use key:value pairs to store data. The collection of values is unordered and does not allow duplicates.
student = {
"name" : "Frida Kahlo",
"SID" : 2394823,
"grade" : 11,
"classes" : ["Biology", "Algebra 2", "Spanish"]
}
This will look very similar to JSON objects or JavaScript objects.
Discussions: