Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet88/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   84   85   86   87   88   89   90   91   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Friday: Creating a Shopping Cart
For today’s project, we’re going to build an application that stores products within a list. We’ll be able to add, remove, clear, and show the products in the cart. All the concepts taught throughout the past few weeks will be used.
To follow along with this lesson, let’s continue from our previous notebook file
Week_05” and add a markdown cell at the bottom that says, “Friday Project: Creating a Shopping Cart.”
CHapTeR 5 FUnCTIonS

Final Design


As we’ve introduced functions this week, the final design will be based around the logic of our program’s actions. Functions perform a specific task, which is usually an action. For our shopping cart program, the actions that we need to consider are the tasks of adding, removing, clearing, and showing the items within the cart. The logical design will look like Figure 5-5.

Figure 5-5. Shopping cart program logic
We’ll be sure to have one main function that will contain the loop and handle user input.

Initial Setup


Like the project from last week, we’ll be creating the program in a single cell, so make sure you familiarize yourself with the concepts we used in that project. To start, let’s import the clearing function from Jupyter Notebook and declare a global variable to work with:

1| # import necessary functions
2| from IPython.display import clear_output
4| # global list variable
5| cart = [ ]

We wanted to declare a global variable of cart to work with throughout this program. We’re going to use a list, as we’ll need to store several items. Using a list will also allow us to edit the variable directly without having to pass it around because of how item assignments work.

Adding Items


As stated in the initial design, we’ll want to create our functions first. We’ll start with the function for adding items to our cart variable:

7| # create function to add items to cart 8| def addItem(item):
9| clear_output( )
10| cart.append(item)
11| print( "{ } has been added.".format(item) )

We won’t call this function until later when we create the main loop. When called, this function will clear the output, append the item passed into the parameter, and output to the user.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   84   85   86   87   88   89   90   91   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish