Python 7a Notes - Updated 1
Python 7a Notes - Updated 1
1 of 14
Contents
The system will first ask user to input the price of each fruit. Thereafter, the system
will display the list on screen for reference:
P. 3 of 14
The next step is user being asked to input the fruit he/she prefers to buy and the
amount would like to buy. See the screen capture below for your reference:
P. 4 of 14
By the time user chooses No for indication of stop buying, the system should show
out the order list and calculate the subtotal and total prices.
P. 5 of 14
As there are 4 kinds of fruits appear in the program, it’s easy to figure out a specific
data structure should be used within the program. In such case, we may consider to
use list or dictionary.
For the current case, dictionary will be more preferable as we can set each fruit
name as the key of the dictionary. For each value of each item in dictionary, we can
put a nested dictionary so that it can set price and order as the keys. This is what we
usually describe as nested dictionary.
Take a look at the diagram below for further elaboration on the structure of
fruitList dictionary:
fruitList.sort()
fruitDict = {}
for f in fruitList:
tempString = "What is the price of " + f + "? "
price = int(input(tempString))
order = 0
innerDict = {}
innerDict["price"]=price
innerDict["order"]=order
fruitDict[f]=innerDict
#print(innerDict)
#print(fruitDict)
flag = True
while flag:
fruitName = input('\nWhat fruit do you want to buy? ').upper()
numOrder = int(input('How many %s do you want to buy? ' %fruitName))
tempDict = fruitDict.get(fruitName)
P. 7 of 14
if tempDict.get("order") > 1:
print('You want to buy %d %sS' %(tempDict.get("order"), fruitName))
else :
print('You want to buy %d %s' %(tempDict.get("order"), fruitName))
cont = input('\nDo you want to buy more (Yes / No) ? ' ).upper()
if cont =="YES":
print("\n-------------------------------------------")
else:
flag = False
else:
print("\n\nThis is your oder:")
total=0
for kk,vv in sorted(fruitDict.items(), reverse = False):
tempOrder = vv.get("order")
print("%s: %d" %(kk, tempOrder))
subTotal = vv.get("order") * vv.get("price")
print("EachPrice: $%d" %vv.get("price"))
print("Subtotal: $%d" %subTotal)
total = total + subTotal
print()
else:
print('==============================================')
print ("Total amount is $%d " %total)
ch = input("")
P. 8 of 14
Elaboration of FruitOrderCase.py
1. Initialization of fruitList
fruitList = ["BANANA", "APPLE", "ORANGE", "GRAPEFRUIT"]
fruitList.sort()
2. Populating fruitDict
fruitDict = {}
for f in fruitList:
tempString = "What is the price of " + f + "? "
price = int(input(tempString))
order = 0
innerDict = {}
innerDict["price"] = price
innerDict["order"] = order
fruitDict[f] = innerDict
Example Input:
If the user provides these prices:
➢ APPLE: $2
➢ BANANA: $1
➢ GRAPEFRUIT: $3
➢ ORANGE: $2
fruitDict = {
"APPLE": {"price": 2, "order": 0},
"BANANA": {"price": 1, "order": 0},
"GRAPEFRUIT": {"price": 3, "order": 0},
"ORANGE": {"price": 2, "order": 0}
}
➢ The program displays the sorted price list of all fruits in ascending order of
their names (reverse=False).
➢ Example Output:
while flag:
fruitName = input('\nWhat fruit do you want to buy? ').upper()
numOrder = int(input('How many %s do you want to buy? ' % fruitName))
tempDict = fruitDict.get(fruitName)
tempDict["order"] = tempDict.get("order") + numOrder
➢ A while loop is used to keep the program running until the user decides to
stop ordering.
➢ Inside the loop:
1. The user is asked for the name of the fruit they want to buy. The input
is converted to uppercase for consistency.
2. The user is asked how many units of the fruit they want to order.
3. The order value in the corresponding fruit's dictionary (tempDict) is
updated by adding the ordered quantity (numOrder).
Example:
If the user orders 2 APPLES:
fruitDict = {
"APPLE": {"price": 2, "order": 2},
"BANANA": {"price": 1, "order": 0},
"GRAPEFRUIT": {"price": 3, "order": 0},
"ORANGE": {"price": 2, "order": 0}
}
P. 11 of 14
➢ If the order value is greater than 1, the fruit name is pluralized by adding an
S.
➢ Otherwise, the singular form of the fruit name is displayed.
if cont == "YES":
print("\n-------------------------------------------")
else:
flag = False
Example Output:
If the user orders:
➢ 2 APPLES
➢ 3 BANANAS
BANANA: 3
EachPrice: $1
Subtotal: $3
GRAPEFRUIT: 0
EachPrice: $3
P. 13 of 14
Subtotal: $0
ORANGE: 0
EachPrice: $2
Subtotal: $0
==============================================
Total amount is $7
P. 14 of 14
Classwork 1
Try to modify program FruitOrderCase.py to FruitOrderCase – validation.py. In the
new program, add some code to validate the Yes / No input by user. If user input
string other than Yes / No, the system should prompt the user to input again. Take
the following screen for your reference before working on Classwork 1.