首页 > 编程语言 >python if语句 5

python if语句 5

时间:2022-12-26 12:36:15浏览次数:44  
标签:语句 requested python age else score user print

1.条件测试

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

for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())

#下面检查不相等  !=
top='mus'
if top!='ac':
    print(top)

#使用and 检查多个条件
age,score=20,80
if age>18 and score>70:
    print(f"{age},{score}")

#使用or 检查多个条件
if age>18 or score>90:
    print(f"{age},{score}")

# 检查列表中是否包含
banned=['andrew','carolina','david']
user='marie'
if user in banned:
    print(f"存在{user}")
if user not in banned:
    print(f"不存在{user}")

#布尔表达式
game_active=True
can_edit=False
if game_active:
    print ("激活游戏")

 2. if 语句

# if elif else
age=12
if age<4:
    print('your admission cost is $0')
elif age<18:
    print('your admission cost is $25')
elif age<20:
    print('your admission cost is $30')
else:
    print('your admission cost is $40')

#测试多个条件
requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
    print("adding mushrooms")
if 'extra cheese' in requested_toppings:
    print("adding extra cheese")
if 'm' in requested_toppings:
    print ("adding m")

 3.使用if语句处理列表

#确定列表不是空的
requested_toppings=[]

if requested_toppings:
    for requested_topping in requested_toppings:
        print(requested_topping)
else:
    print ("list is empty")

#使用多个列表, 获取requestes元素存在于avaliables中的
avaliables=['mushrooms', 'olives', 'green peppers',
                        'pepperoni', 'pineapple', 'extra cheese']
requestes= ['mushrooms', 'french fries', 'extra cheese']

for  avaliable in avaliables:
    if avaliable in requestes:
        print(f"adding {avaliable}")
    else:
        print(f"we don't have {avaliable}")

 

标签:语句,requested,python,age,else,score,user,print
From: https://www.cnblogs.com/MrHSR/p/16386225.html

相关文章

  • python 字典 6
    1.字典增删改#简单字典用法,用{}表示字典。键和值之间用冒号分隔,而键值对之间用逗号分隔,如下所示alien_0={'color':'green','points':'5'}print(alien_0['color'])#字......
  • python while 7
    1.简单的while示例current_number=1whilecurrent_number<=5:print(current_number)current_number+=1 2.使用标志active=Truenum=1num_end=10w......
  • python 文件操作 11
    一.文件读取操作1.读取整个文件在同级目录,创建一个pi_digits.txt文件和file_reader.py文件。pi_digits.txt文件中加入内容file_reader.py文件内容如下:w......
  • python 异常处理 12
    当python程序在执行期间发生错误时,如果编写了处理该异常的代码,程序将继续运行;如果未对异常进行处理,程序将停止并显示traceback,其中包含有关异常的报告。异常是使用try......
  • python 多版本查看与命令用法
    1.windows查看电脑上是否有多个版本 如果python2能查到,那么用命令时1、pip是python的包管理工具,pip和pip3版本不同,都位于Scripts\目录下:2、如果系统中只安装了Python......
  • Centos7.8误删Python2.7之后,导致yum和Python命令无法使用
    Centos7.8误删Python2.7之后,导致yum和Python命令无法使用先简单介绍下我的情况与背景:我在昨天写一个模块,跑Python脚本报错,由于我不熟习Python2,3之间语法有差异,导致......
  • python之路56 csrf跨站请求 auth模块登录注册方法
    csrf跨站请求伪造钓鱼网站:模仿一个正规的网站让用户在该网站上做操作但是操作的结果会影响到用户正常的网站账户但是其中有一些猫腻eg:英语四六级考试需要网上先......
  • python-操作符
    1.python-操作符有什么用操作符图解操作符:一个特定的符号,用它与其他数据类型连接起来组成一个表达式。常用于条件判断,根据表达式返回True/False采取动作。2.比......
  • python-条件判断
    1.python-条件判断条件判断流程图语法格式if<表达式>:<代码块>elif<表达式>:<代码块>else:<代码块>条件判断-单分支语句示例:判断是否成年age......
  • python-循环
    1.python-循环在了解编程中的“循环”之前,先试想下这个场景:在阳台种花,准备种4颗种子,开始逐个挖坑,放一颗种子。每一颗种子操作都是相同的,如果我们用一步将6颗种子重......