Document the program below using Python comments. class KeychainOrder: def __ini
ID: 3572808 • Letter: D
Question
Document the program below using Python comments.
class KeychainOrder:
def __init__(self, price, num=0):
self.price = price
self.num = num
def addKeychains(self, num):
self.num += num
print("Keychains in order:", self.num)
def removeKeychains(self, num):
if(num<self.num):
self.num -= num
print("Keychains in order:", num)
else:
print("Don't have that many key chains in order.")
def getKeyChains(self):
return self.num
def checkout(self):
print("You have " + str(self.num) + " keychains.")
print("Key chain cost $" + str(self.price) + "each.")
print("Total cost is $" + str(self.price*self.num))
print("Thank you for your order!")
def __str__(self):
return "You have " + str(self.num) + " keychains. The total cost is $" + str(self.num*self.price)
def main():
keychainorder = KeychainOrder(20)
while(True):
choice = int(input("1. Add keychains to order 2. Remove keychains from order 3. View current order 4. Checkout "))
if choice == 1:
num = int(input("Number of keychains: "))
keychainorder.addKeychains(num)
elif choice == 2:
num = int(input("Number of keychains: "))
keychainorder.removeKeychains(num)
elif choice == 3:
print(keychainorder.__str__())
elif choice == 4:
keychainorder.checkout()
break
else:
print("Please enter a number between 1 to 4.")
main()
Explanation / Answer
class KeychainOrder:
#contructor with params self, price and num as 0
def __init__(self, price, num=0):
self.price = price#setting the price as price
self.num = num#setting the num as price
#method definition of addKeychains(), params as self and num. num will be added to current num
def addKeychains(self, num):
self.num += num
print("Keychains in order:", self.num)
#method defination of removeKeychains(), params as self and num. num will be subtracted to current num if(num<self.num)
def removeKeychains(self, num):
if(num<self.num):
self.num -= num
print("Keychains in order:", num)
else:
print("Don't have that many key chains in order.")
#method definition of getKeyChains to get the number of key chain
def getKeyChains(self):
return self.num
#method definition of checkout to check out the current order
def checkout(self):
print("You have " + str(self.num) + " keychains.")
print("Key chain cost $" + str(self.price) + "each.")
print("Total cost is $" + str(self.price*self.num))
print("Thank you for your order!")
# to string methof to return the string
def __str__(self):
return "You have " + str(self.num) + " keychains. The total cost is $" + str(self.num*self.price)
# main function, this is called first
def main():
keychainorder = KeychainOrder(20)#creating a object of KeychainOrder with reference variable keychainrrder
#infinte loop for the menu
while(True):
#taking input from the user based on the menu
choice = int(input("1. Add keychains to order 2. Remove keychains from order 3. View current order 4. Checkout "))
#if choice is 1 the get the input from the user for the number of keychains
if choice == 1:
num = int(input("Number of keychains: "))
keychainorder.addKeychains(num)#invoke the addKeychains method of keychainorder class with the value num
#if choice is 2 the get the input from the user for the number of keychains and remove it
elif choice == 2:
num = int(input("Number of keychains: "))
keychainorder.removeKeychains(num)#invoke the removeKeychains method of keychainorder class with the value num
#if choice is 3 then print the current order
elif choice == 3:
print(keychainorder.__str__())#invoke the __str__() method of keychainorder class to print the current order
#if choice is 4 then check out the current order
elif choice == 4:
keychainorder.checkout()#invoke the checkout() method of keychainorder class to check out the current order
break# break the loop on this
#deafault condition when the choice is not between 1 to 4
else:
print("Please enter a number between 1 to 4.")
main()
-------output-------
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
1
Number of keychains: 2
('Keychains in order:', 2)
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
1
Number of keychains: 2
('Keychains in order:', 4)
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
3
You have 4 keychains.
The total cost is $80
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
2
Number of keychains: 1
('Keychains in order:', 1)
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
3
You have 3 keychains.
The total cost is $60
1. Add keychains to order
2. Remove keychains from order
3. View current order3
You have 3 keychains.
The total cost is $60
1. Add keychains to order
2. Remove keychains from order
3. View current order
4. Checkout
4
You have 3 keychains.
Key chain cost $20each.
Total cost is $60
Thank you for your order!
----output----------------
Note the program has been properly documented. Also please keep in mind of the indentation in python.
Please go through it and ask doubts if any. God bless you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.