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

相关推荐
Dxy123931021613 小时前
Python使用正则提取字符串中的数字
python
wbs_scy13 小时前
Makefile 完全指南:从入门到工程化,自动化构建不再难
运维·自动化
yqj23413 小时前
【无标题】
java·开发语言
之歆13 小时前
Linux 网络配置与 TCP/IP 协议
linux·网络·tcp/ip
花果山总钻风13 小时前
SQLAlchemy各种排序示例
后端·python·中间件
DeeplyMind13 小时前
第11章 容器运行参数详解
运维·docker·容器
REDcker13 小时前
curl开发者快速入门
linux·服务器·c++·c·curl·后端开发
游乐码13 小时前
c#结构体
开发语言·c#
成震197113 小时前
UBUNTU 安装虚拟机
linux·运维·ubuntu
Coder_Boy_13 小时前
JDK17_JDK21并发编程:资深架构常用模式+最佳实践
java·开发语言·spring boot·架构