Fully Qualified Names FQNs prevent your programs from getting confused

Imagine if you lived in a world where people had first names only:

Michael

Michael

Michael

Lots of people share the same first name. But people also have surnames. If you use a first name with a surname, things are a lot less confusing.

And it's the same thing with code. If you have two modules containing functions with the same name, the computer will get confused. But if you fully qualify the function name, by prefixing it with the module name, the computer will know exactly what you mean:

promotion.discount(1.75)

If you are going to use Fully Qualified Names (FQNs) from a module, then you will also need to change the way you import the code:

from prom import promotion

Michael

Oh, I need to apply the 10% discount from promotion.py? That's not a problem, since you're using a FQN...

.This will import the code from p^^Wf/ w but to use it, you will need to add Kpromotion-to the start of the funcW name'

coffee_pos.py

Now you can fix the code to use both discounts.

smarter pos m

These are the two discount modules:

pvomo-boN-fY

stârbi

UZi.py

# Official Starbuzz Discount Module

# Copyright(c) Starbuzz Corporation

# All Rights Reserved.

# This function calculates a 5% discount on a price def discount(price):

return 0.95 * price

Write a new version of coffee_pos.py that, after choosing a menu option, will ask if the customer has a Starbuzz Discount Card. If the answer is "Y", apply both the Starbuzz and the promotion discount. Otherwise, just apply the promotion discount.

Here is the latest version of coffee_pos. py from transactions import * from promotion import *

items - ["DONUT", "LATTE", "FILTER", "MUFFIN"] prices - [1.50, 2.20, 1.80, 1.20] running - True while running: option - 1 for choice in items:

print(str(option) + ". " + choice) option - option + 1 print(str(option) + ". Quit") choice - int(input("Choose an option: ")) if choice -- option: running - False else:

credit_card - input("Credit card number: ") new_price - discount(prices[choice - 1])

save_transaction(new_price, credit_card, items[choice - 1])

Vtfv'rte 7°u'r here. Vi getting a promotion m to rig Excise

These are the two discount modules:

starbi

KZi.py

# Official Starbuzz Discount Module

# Copyright(c) Starbuzz Corporation

# All Rights Reserved.

# This function calculates a 5% discount on a price def discount(price):

return 0.95 * price

You were asked to write a new version of coffee_pos. py that, after choosing an menu option, will ask if the customer has a Starbuzz Discount Card. If the answer is "Y", apply both the Starbuzz and the promotion discount. Otherwise, just apply the promotion discount.

Here is the latest version of coffee_pos .py:

from transactions import * from promotion import *

items = ["DONUT", "LATTE", "FILTER", "MUFFIN"] prices = [1.50, 2.20, 1.80, 1.20] running = True while running: option = 1 for choice in items:

print(str(option) + ". " + choice) option = option + 1 print(str(option) + ". Quit") choice = int(input("Choose an option: ")) if choice == option: running = False else:

credit_card = input("Credit card number: ") new_price = discount(prices[choice - 1])

save_transaction(new_price, credit_card, items[choice - 1])

By importing the transactions module like this, from transactions import * you can call the functions without the module name. import promotion

Buzz.

import starbu You need to use this kind of \

import for "promotionpy and "starbuzz-py", because you are going to' qualify the .. .items . . . = . .[".DONU.T", . "LATTE",. "FILTER",. "MUFFIN"].

function names with the module names. prices = [1.50, 2.20, 1.80, 1.20]

running = True while running: option = 1 for choice in items:

print(str(option) . +..".. . ". + . choice)

choice . = .int(input(".C.ho.ose. . an . option:. . "))

if choice = option:

running = False else:

cr.ed.i.t_card. .=. .input("Credit. .card . number: . ")..

If someone.has a price = promotion.discount(prices[choice - 1]) Starbuzz. Discount

Card, you need ^ if mput("Starbuzz card? ") == "Y":

apply the second

Starbuzz. discount. price = starbuzz.discount(price)

say.e_tr.an.s.action(price,. .credit_ca.rd,. . items[choice . - . .'])..

Was this article helpful?

0 0

Post a comment