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



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

Removing Items


Next, we’ll create the function that will remove items from our cart variable:
13| # create function to remove items from cart 14| def removeItem(item):
15| clear_output( ) 16| try:
17| cart.remove(item)
18| print( "{ } has been removed.".format(item) ) 19| except:
20| print("Sorry we could not remove that item.")
We want to be sure to include a try and except clause around the remove statement because when removing an item that doesn’t exist, the program would crash. This prevents that occurrence and will either remove the item properly or output to the user that it didn’t work.
CHapTeR 5 FUnCTIonS

Showing the Cart


We want the user to be able to view the cart at any time, which uses a simple loop:

22| # create a function to show items in cart 23| def showCart( ):
24| clear_output( ) 25| if cart:
26| print("Here is your cart:") 27| for item in cart:
28| print( "- { }".format(item) ) 29| else:
30| print("Your cart is empty.")

Within the function, we clear the output first, then check to see if there are items within the cart. If it’s empty, we let the user know; otherwise, we’ll loop over the items and output one per line.

Clearing the Cart


One of the last functions we need is the ability to clear the cart:

32| # create function to clear items from cart 33| def clearCart( ):
34| clear_output( )
35| cart.clear( )
36| print("Your cart is empty.")

Using the built-in clear method, we clear the cart of all items and let the user know.

Creating the Main Loop


So far, we’ve been creating the functions for handling the user’s actions. Now we need to set up the program’s main function which will contain the master loop and ending functionality:

38| # create main function that loops until the user quits 39| def main( ):
40| done = False 42| while not done:
43| ans = input("quit/add/remove/show/clear: ").lower( )
45| # base case 46| if ans == "quit":
47| print("Thanks for using our program.")
48| showCart( )
49| done = True
51| main( ) # run the program

Go ahead and run the cell. You should now be able to type in “quit” and exit the program; otherwise, it will continue to run. We haven’t set up what to do other than exiting; however, we’ve made sure that our base case is set up properly, as to not create an infinite loop. We also use the boolean variable done in order to keep track of whether the master loop is complete.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   85   86   87   88   89   90   91   92   ...   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