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实验,证明该方法是有效的,但本文的方法单一,可实现的成果简单单一,未来可以在此基础上扩展自主售卖机的功能。

相关推荐
用户2519162427111 小时前
Python之语言特点
python
刘立军1 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
JuiceFS2 小时前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
数据智能老司机5 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
CYRUS_STUDIO5 小时前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
数据智能老司机6 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i7 小时前
django中的FBV 和 CBV
python·django
c8i7 小时前
python中的闭包和装饰器
python
熊猫李7 小时前
rootfs-根文件系统详解
linux
chen9458 小时前
mysql 3节点mgr集群部署
运维·后端