首页 > 编程语言 >python编程从入门到实践--第5章 if语句

python编程从入门到实践--第5章 if语句

时间:2022-10-10 19:46:53浏览次数:68  
标签:requested topping python car 编程 mushrooms -- toppings print

一。条件测试

  符号:==, >, >=, <, <=, !=, 

       逻辑符号:and, or, not

  测试有没在列表中

cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:
    if car != "subaru" and car != "toyota":
        print("German car")
        if car == "bmw":
            print(car.upper())
        else:
            print(car)
    else:
        print("Japanese car")
        if not car == "subaru":
            print("丰田")
        else:
            print("斯巴鲁")

# 测试是在列表中
requrested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requrested_toppings)

# 测试不在列表中
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(f'{user.title()}, you can post a response if you wish.')

二。多分支

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 25
elif age < 65:
    price = 40
elif age >= 65:
    price = 20
else:
    price = -1

print(f"Your adminssion cost is ${price}.")

三。多条件

requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
    print('Add mushrooms.')
if 'pepperoni' in requested_toppings:
    print('Add pepperoni.')
if 'extra cheese' in requested_toppings:
    print('Adding extra cheese.')

print('\nFinished making your pizza!')

四。处理列表

1。确定列表不为空,检查特殊元素。

requested_toppings = ['mushrooms', 'green prppers', 'extra cheese']

# 检查列表不为空
if requested_toppings:
    for requested_topping  in requested_toppings:
        if requested_topping == 'green peppers':
            print("Sorry, we are out of green peppers right now.")
        else:
            print("Adding {requested_topping}.")    
    print("\nFinished makeing your pizza!")
else:
    print("Are you sure you want a plain pizza?")

2。使用多个列表

available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'green prppers', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print(f"Adding {requested_topping}.")
    else:
        print(f"Sorry, we don't have {requested_topping}")

print("\nFinished making your pizza!")

 

标签:requested,topping,python,car,编程,mushrooms,--,toppings,print
From: https://www.cnblogs.com/duju/p/16776904.html

相关文章

  • webpack模块化的原理
    commonjs在webpack中既可以书写commonjs模块也可以书写es模块,而且不用考虑浏览器的兼容性问题,我们来分析一下原理。首先搞清楚commonjs模块化的处理方式,简单配置一下webp......
  • 四则运算
    //这个代码吧略有缺陷四则运算的乘除与加减只能从左到右依次计算还没想好怎么做importjava.util.Random;importjava.util.Scanner;publicclasstest{publicst......
  • IDEA的基本使用:让你的IDEA有飞一般的感觉
    IDEA的基本使用:让你的IDEA有飞一般的感觉来自:CSDN,作者:琦彦链接:https://blog.csdn.net/fly910905/article/details/77868300目录1.设置maven2.IDEA设置代码行宽度3.IDEA......
  • Leecode104 二叉树的最大深度
    //DFS解法前序遍历/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*Tr......
  • gitlap上面fork了别人的仓库,当源仓库有更新了,自己的本地仓库和自己的远程仓库怎么更新
      如图:自己的远程仓库是origin,fork的源仓库命名为source。当source仓库有发生更新时,怎么推送到我们的origin?方法如下:第1步:gitfetchsourcedevelop//从远程......
  • 另一个四则运算
    importcom.sun.javaws.IconUtil;//这个跟那个差不多进步在于类至于三年级没写道理相同importjava.util.Scanner;publicclassMain{publicstaticvoidmain(St......
  • java spring 纯注解开发
     创建核心容器有两个方法如下图     获取Bean对象方法有三种     BeanFactory与FactoryBean区别    spring纯注解由哪些常见的 ......
  • 洛谷 P3488 [POI2009]LYZ-Ice Skates 题解
    错解每次跑二分图匹配,时间复杂度显然爆炸。时间复杂度:我被杀手皇后摸过了正解引入Hall定理:设二分图中\(G=<V_1,V_2,E>,|V_1|\le|V_2|\),则G中存在\(V_1\)到......
  • 高代技巧小记
    行列式的补行补列技巧扰动法对于二阶分块矩阵\(\left[\begin{matrix}A&B\\C&D\end{matrix}\right]\),若\(AC=CA\),则\(\left|\begin{matrix}A&B\\C&D\end{ma......
  • stm32cubemx配置f103zet6定时器中断,pwm波输出模式,输出捕获模式 慢慢更新
    定时器详细介绍(基于标准库的,原理部分可参考): STM32-定时器详解_KevinFlyn的博客-CSDN博客_stm32定时器1.定时器中断配置配置参考 STM32CubeMX配置定时器中断_小哥。的......