饮品自动售卖机python的代码
时间: 2023-07-23 11:18:50 浏览: 72
以下是一个简单的饮品自动售卖机的 Python 代码:
```python
# 饮品自动售卖机
# 定义饮品和价格
drinks = {
"可乐": 3,
"雪碧": 3,
"绿茶": 2.5,
"红茶": 2.5,
"咖啡": 5
}
# 打印菜单
print("欢迎使用饮品自动售卖机!")
print("以下是我们的菜单:")
for drink, price in drinks.items():
print(drink, price)
# 输入金额
money = float(input("请投入硬币或纸币:"))
# 选择饮品
drink = input("请选择您要购买的饮品:")
if drink not in drinks:
print("对不起,我们没有这种饮品。")
else:
price = drinks[drink]
if money < price:
print("对不起,您的金额不足。")
else:
change = money - price
print("购买成功!找您", change, "元。")
```
这个代码定义了一个包含饮品和价格的字典,然后打印了菜单。用户需要输入金额和选择饮品。如果选择的饮品不存在,或者金额不足,会提示用户相应的信息。如果购买成功,会计算出找零并输出。
相关问题
饮品自动售卖机python的代码,while循环
以下是一个简单的饮品自动售卖机的Python代码,使用while循环来实现:
```
# 定义饮品和价格
drinks = {
"可乐": 3,
"雪碧": 2.5,
"矿泉水": 1.5
}
# 输出饮品列表
print("欢迎使用饮品自动售卖机!请选择您需要的饮品:")
for drink, price in drinks.items():
print(drink + " 价格:" + str(price) + "元")
# 循环读取用户输入
while True:
choice = input("请输入饮品名称:")
# 判断用户输入是否合法
if choice not in drinks:
print("无效的输入,请重新输入!")
continue
# 计算价格并输出
price = drinks[choice]
print("您选择了" + choice + ",需要支付" + str(price) + "元。")
# 循环读取用户支付金额
while True:
payment = input("请投入纸币或硬币:")
try:
payment = float(payment)
except ValueError:
print("无效的输入,请重新输入!")
continue
if payment < price:
print("金额不足,请继续投币!")
else:
change = payment - price
print("找零:" + str(change) + "元。谢谢惠顾!")
break
# 询问用户是否继续购买
buy_again = input("是否继续购买?(y/n)")
if buy_again.lower() == "y":
continue
else:
break
```
上述代码中,第一个while循环用于循环读取用户输入并判断输入是否合法,第二个while循环用于循环读取用户支付金额并判断金额是否足够,如果用户支付金额不足,则需要继续循环读取。最后,代码询问用户是否继续购买,如果用户选择继续购买,则继续循环读取用户输入,否则退出循环。
饮品自动售货机python代码
饮品自动售货机的Python代码可以如下所示:
```python
class VendingMachine:
def __init__(self):
self.products = {
'Coke': 10,
'Pepsi': 8,
'Sprite': 5
}
self.prices = {
'Coke': 2.5,
'Pepsi': 2.0,
'Sprite': 1.5
}
self.balance = 0.0
def display_products(self):
for product, quantity in self.products.items():
print(f'{product}: {quantity}')
def insert_coin(self, amount):
self.balance += amount
def purchase(self, product):
if product not in self.products:
print("Invalid product!")
return
if self.products[product] == 0:
print("Out of stock!")
return
if self.balance < self.prices[product]:
print("Insufficient balance!")
return
self.products[product] -= 1
self.balance -= self.prices[product]
print(f"Purchased {product} successfully!")
def get_change(self):
change = self.balance
self.balance = 0.0
return change
# 示例使用
vending_machine = VendingMachine()
vending_machine.display_products()
vending_machine.insert_coin(5.0)
vending_machine.purchase('Coke')
change = vending_machine.get_change()
print(f"Change: {change}")
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)