Python实现自主售卖机

1 问题

在python中我们常常使用到条件判断,if语句时常见的条件判断语句之一。那么如何使用if语句实现根据情况自动选择商品进行售卖呢?

2 方法

根据if语句执行时从上往下执行的特点,使用if语句、dict和list来实现整个流程**。**

代码清单 1

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| drinks = {'greentea':'please pay 4','weaktea':'please pay 5','blackcffee':'please pay 7','coffeewithmilk':'please pay 9','cocoa':'please pay 6'} drink = input('please input you wanted drink:') L=['greentea','weaktea','blackcffee','coffeewithmilk','cocoa'] if drink == l[0]: print( drinks['greentea']) elif drink == l[1]: print( drinks['weaktea']) elif drink == l[2]: print( drinks['blackcffee']) elif drink == l[3]: print( drinks['coffeewithmilk']) elif drink == l[4]: print( drinks['cocoa']) else: print('There is no drink you want') |

3 结语

针对用python实现自主售卖机问题,提出if语句的方法,通过运用if语句、dict和list实验,证明该方法是有效的,但本文的方法单一,可实现的成果简单单一,未来可以在此基础上扩展自主售卖机的功能。

相关推荐
十日十行15 小时前
Linux和window共享文件夹
linux
AI探索者16 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者16 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh18 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅18 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽19 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
Sinclair19 小时前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
木心月转码ing1 天前
WSL+Cpp开发环境配置
linux
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python