python - 自动售货机项目 - OOP 代码指南 [Python]
问题描述
对编程很陌生,我的一些 python 代码有问题。我觉得会有一种更简单的方法来编写/简化它。我仍在处理它(之前处理第一个项目,并且在虚拟自动售货机中得到了我的“茶”。但是我不确定使用那么多 if elif 语句是完成代码的最干净的方法吗?另外,我想要该代码不断为用户提供输入,以便他们可以订购更多的一种饮料(如果他们有钱的话)。所以我希望它循环并重新开始而不会丢失硬币,我该怎么做?
我知道这并不多,但这是我的第一个任务,我很高兴我能做到这一点!
class Vending_Machine:
aussie_coins = (0.05, 0.10, 0.20, 0.50, 1.00, 2.00)
items = ['Tea','Coffee', 'Coke', 'Orange Juice']
item_price = [0.50, 1.00, 1.50, 1.00]
item_code = ['1', '2', '3', '4']
def __init__(self): #define total in vending machine.
self.total = 0.00
def insert_coin(self,coin):
if float(coin) not in (self.aussie_coins):
print ('The Vending Machine accepts only: {}. ' .format(self.aussie_coins), end = '')
else:
self.total += coin
print ('Currently there is a total of {: .2f} in machine' .format(self.total))
class interface(Vending_Machine):
def menu(self):
print("##################################")
print(" Welcome to my Vending Machine ")
print("All items below are readily available")
print(Vending_Machine.item_code[0], Vending_Machine.items[0], "${: .2f}".format(Vending_Machine.item_price[0])) #
print(Vending_Machine.item_code[1], Vending_Machine.items[1], "${: .2f}".format(Vending_Machine.item_price[1]))
print(Vending_Machine.item_code[2], Vending_Machine.items[2], "${: .2f}".format(Vending_Machine.item_price[2]))
print(Vending_Machine.item_code[3], Vending_Machine.items[3], "${: .2f}".format(Vending_Machine.item_price[3]))
print("##################################")
class user_input(interface):
def choice (self):
choice = input("Please enter the item code of an item you would like to purchase: ")
if choice == Vending_Machine.item_code[0]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[0], Vending_Machine.item_price[0], self.total))
if self.total < Vending_Machine.item_price[0]:
coins = float(input("Insert a coin into the vending machine: "))
Vending_Machine.insert_coin(self,coins)
if self.total == Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. There is currently ${: .2f} left in the Machine. Thanks, have a nice day!'.format(Vending_Machine.items[0], self.total))
elif self.total > Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print ('Please take your {}. There is currently ${: .2f} left in the Machine. Thanks, have a nice day!'.format(Vending_Machine.items[0], self.total))
elif self.total > Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[0],self.total))
elif self.total == Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[0]))
elif choice == Vending_Machine.item_code[1]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[1], Vending_Machine.item_price[1], self.total))
if self.total < Vending_Machine.item_price[1]:
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[1],self.total))
elif self.total == Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[1]))
elif choice == Vending_Machine.item_code[2]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[2], Vending_Machine.item_price[2], self.total))
if self.total < Vending_Machine.item_price[2]:
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[2],self.total))
elif self.total == Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[2]))
elif choice == Vending_Machine.item_code[3]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[3], Vending_Machine.item_price[3], self.total))
if self.total < Vending_Machine.item_price[3]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[3],self.total))
elif self.total == Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[3]))
elif choice not in item_code:
print("Sorry we do not have item number {} available. Please try again" .format(choice))
vm = Vending_Machine()
i1 = interface()
u1 = user_input()
i1.menu()
u1.choice()
解决方案
干得好。我有一个词要对你说:循环。
如果您有 4 行打印自动售货机项目(在开始时),您可以用循环替换它,如下所示:
for index in range(len(VendingMachine.items)):
print(Vending_Machine.item_code[index], Vending_Machine.items[index], "${: .2f}".format(Vending_Machine.item_price[index]))
希望这可以为您提供足够的信息来确定如何缩小其余代码。如果需要elif
(例如,您只想检查其中一个,而不是全部),break
请在循环中使用 a(请参阅下面的中断示例)。
至于您的第二个问题,您可以围绕insert_coin
函数调用(在user_input
函数中)进行循环,该循环不断添加硬币,直到用户输入“完成”。
这是一个非常基本的例子。可能有更好的方法来做到这一点,但它应该工作。
while True:
coins = float(input("Insert a coin into the vending machine: "))
if coins == "done":
break
Vending_Machine.insert_coin(self,coins)
推荐阅读
- r - 受访者的答案不一致 - 给出多个答案时保持最低答案?在 R Tidyverse 中
- r - 根据日期合并两个长格式数据帧
- python - - 不支持的操作数类型:'datetime.time' 和 'datetime.time' 当我计算 OT 小时数和总小时数时
- python - Python Matplotlib:在 Windows 上将 matplotlib 更新到 3.1.2 的问题
- java - 在自己的类中实例化对象
- arrays - 模板只包含上下文传递的数组的最后一个元素
- r - 部分 pivot_longer 的更好解决方案
- javascript - 如何使用 VeeValidate 3 和 vue.js 制定自定义验证规则
- r - R countif 并在指定向量中匹配元素的多列上求和
- alexa - Alexa技能错误“请求的技能响应有问题”