首页 > 其他分享 >第五章 if语句

第五章 if语句

时间:2023-03-21 15:33:29浏览次数:33  
标签:语句 requested age else 第五章 toppings print price

一个简单示例

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

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())
# Audi
# BMW
# Subaru
# Toyota

条件测试

条件测试: 每条if 语句的结果要么是True ,要么是False ;

Python根据条件测试的值为True 还是False 来决定是否执行if 语句中的代码

A == B:A等于B,则结果为True,否则为False

A != B: A不等于B,则结果为True,否则为False

requested_topping = 'mushrooms'

if requested_topping != 'anchovies':
	print("Hold the anchovies!")
# Hold the anchovies!

条件语句中可包含各种数学比较,如小于(<)、小于等于(<=)、大于(>)、大于等于(>=)

# 使用and 检查多个条件
age = 30
if age > 18 and age < 60:
  print("good")
# good

# 使用 or 检查多个条件
age = 9
if age > 18 or age < 10:
  print("good_2")
# good_2

要判断特定的值是否已包含在列表中,可使用关键字in;

要判断特定的值是否未包含在列表中,可使用关键字not in

布尔表达式的结果要么为True ,要么为False ;

布尔值通常用于记录条件,如游戏是否正在运行,或用户是否可以编辑网站的特定内容

if语句

if conditional_test:
	do something
# 如果条件测试的结果为True ,Python就会执行紧跟在if语句后面的代码;
# 否则Python将忽略这些代码

age = 19
if age >= 18:
  print("You are old enough to vote!")
  print("Have you registered to vote yet?")
# You are old enough to vote!
# Have you registered to vote yet?

# if-else 结构
age = 17
if age >= 18:
  print("You are old enough to vote!")
  print("Have you registered to vote yet?")
else:
  print("Sorry, you are too young to vote.")
  print("Please register to vote as soon as you turn 18!")
# Sorry, you are too young to vote.
# Please register to vote as soon as you turn 18!

# if-elif-else 结构
age = 12

if age < 4:
	price = 0
elif age < 18:
	price = 5
else:
	price = 10
    
print("Your admission cost is $" + str(price) + ".")
# Your admission cost is $5.

# 使用多个elif 代码块
age = 12

if age < 4:
	price = 0
elif age < 18:
	price = 5
elif age < 65:
	price = 10
else:
	price = 5
    
print("Your admission cost is $" + str(price) + ".")

# 省略else 代码块
age = 12

if age < 4:
	price = 0
elif age < 18:
	price = 5
elif age < 65:
	price = 10
elif age >= 65:
	price = 5
    
print("Your admission cost is $" + str(price) + ".")
# 该处的elif 代码块在顾客的年龄超过65(含)时,就将价格设置为5美元;
# 这比使用else 代码块更清晰些
# 经过这样的修改后,每个代码块都仅在通过了相应的测试时才会执;
# else 是一条包罗万象的语句,只要不满足任何if 或elif 中的条件测试,其中的代码就会执行,这可能会引入无效甚至恶意的数据;
# 如果知道最终要测试的条件,应考虑使用一个elif代码块来代替else代码块;
# 这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行

# 测试多个条件
requested_toppings = ['mushrooms', 'extra cheese']

if 'mushrooms' in requested_toppings:
	print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
	print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
	print("Adding extra cheese.")
    
print("\nFinished making your pizza!")
# Adding mushrooms.
# Adding extra cheese.
# Finished making your pizza!

使用if语句处理列表

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
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 making your pizza!")
# Adding mushrooms.
# Sorry, we are out of green peppers right now..
# Adding extra cheese.

# Finished making your pizza!

# 确定列表是否未空
requested_toppings = []

if requested_toppings:
	for requested_topping in requested_toppings:
		print("Adding " + requested_topping + ".")
	print("\nFinished making your pizza!")
else:
	print("Are you sure you want a plain pizza?")
# Are you sure you want a plain pizza?

# 使用多个列表
available_toppings = ['mushrooms', 'olives', 'green peppers',
					 'pepperoni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'french fries', 'extra cheese']

for requested_topping in requested_toppings:
	if requested_topping in available_toppings:
		print("Adding " + requested_topping + ".")
	else:
		print("Sorry, we don't have " + requested_topping + ".")
        
print("\nFinished making your pizza!")
# Adding mushrooms.
# Sorry, we don't have french fries.
# Adding extra cheese.
# Finished making your pizza!

设置if语句的格式

在条件测试的格式设置方面,PEP 8提供的唯一建议是:

在诸如== 、>= 和<= 等比较运算符两边各添加一个空格

例如,if age < 4: 要比 if age<4: 好;

这样的空格不仅不会影响Python对代码的解读,还会让代码阅读起来更容易

小结

条件测试;if、if-else、if-elif-else结构;
Python在代码格式方面的建议,这可确保即便你编写的程序越来越复杂,但其代码依然易于阅读和理解

标签:语句,requested,age,else,第五章,toppings,print,price
From: https://www.cnblogs.com/artwalker/p/17240181.html

相关文章

  • SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    Insert是T-sql中常用语句,InsertINTOtable(field1,field2,...)values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要......
  • SQL语句查询
    MySQL  SELECT使用“*”查询表的所有字段SELECT可以使用“*”查找表中所有字段的数据,语法格式如下:SELECT*FROM表名;查询表中的某一个字段的语法格式为:SELECT<......
  • 软件工程日报——SQL语句查询不能应用在jsp文件
    今天上课我们在课堂上讲解了如何搞对象,然后再后两节课我和我的伙伴进行了对地铁系统中初始站台和终点站台查询功能的研究。在这个过程中,我们发现一个问题:能够实现web页面查......
  • 实验2 C语言输入输出和控制语句应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0));......
  • resources目录下的mapper写sql语句没有提示
    resources目录下的mapper写sql语句没有提示首先了解一下mybatix-config.xml连接mapper文件的三种方式:<!--第一种--><mapperresource="com/bbl/dao/UserMa......
  • 【Shell 编程】变量详解 | 特殊变量与标准变量 | 基本语句介绍
     ......
  • switch case 语句
    语法格式switch(expression){casevalue://语句break;//可选casevalue://语句break;//可选//你可以有任意数量的c......
  • BOSS语句整理!!!花了两个小时!!!
    一、前置条件-文本类字段1.文本类字段设置前置条件一定要用三段式:前端看到文本类字段为空,数据库里有时储存的是一个空格符①文本不为空:文本<>nulland文本<>''and文......
  • goto语句
    形式:goto表达形式;表达形式:————注意冒号表达形式:————可在任何位置比如:#include<stdio.h>intmain(){ //again: printf("wwwww\n"); gotoagain;//goto后面为a......
  • python语句之列表推导式
    python语句之列表推导式列表推导式是python语言特有的一种语法结构,也可以看成是python中独特的数据处理方法它在python中用转换和过滤数据语法格式:[表达......