首页 > 其他分享 >条件

条件

时间:2023-11-29 23:11:30浏览次数:23  
标签:cet4 cet6 return else abs 条件 print

条件

if语句

绝对值

def abs2(n):
	if n<0:
		n=-n
	return n

if-else 语句

x=input("x")
x=float(x)
print("hello")
if x<10:
	print("wa")
else:
	print("ro")
print("goodbye")

x=10

hello

ro

goodbye

重新设计abs

def abs(n):
if n>0:
	sign=+1
else:
	sign=-1
return sign*n

if-elif-else

可以用多个elif

def getgrade(score):
	if score>=90:
		grade="A"
	elif score>=80:
		grade="B"
	elif score>=70:
		grade="C"
	elif score>=60:
		grade="D"
	else:
		grade="F"
	

if-else推导式

def abs(n):
	return n if (n>=0) else -n


def abss(n):
	if n>=0:
		return n
	else:
		return -n

print("abs(5)=",abs(5),"and abs(-5)=",abs(-5))

abs(5)=5 and abs(-5)=5

match-case语句

def http_error(ststus):
	match status:
		case 400:
			return "bad request"
		case 404:
			return "not found"
		case 418:
			return "i an a teapot"
		case _:
			return "something is wrong"

练习

def is_graduate(cet4,cet6):
	if cet4>=425:
		if cet6>=425:
			return "yes"
		else:
			return "no"
	else:
		return "no"
	cet4,cet6=input().split()
	
	cet4=float(cet4)
	cet6=float(cet6)
	
	print(is_graduate(cet4,cet6))

改进

def is_graduate(cet4,cet6):
	if cet4>=425 and cet6>=425
		
		return "yes"
		
	else:
		return "no"
	cet4,cet6=input().split()
	
	cet4=float(cet4)
	cet6=float(cet6)
	
	print(is_graduate(cet4,cet6))
	

进阶

def is_graduate(cet4,cet6):
	if cet4>=425 and cet6>=425
		
		return "yes"
		
	else:
		return "no"
	cet=input()
	if " " in cet:
		cet4,cet6=input().split()
		cet4=float(cet4)
		cet6=float(cet6)
	else:
		cet4=float(cet)
	    cet6=None
	    
	print(is_graduate(cet4,cet6))
	

	

清晰的代码风格

else部分

b=True

if b:

	print("yes")

else:

	print("no")



空白if部分

b=False
if not b:
	print("no")

用嵌套if 而非and 判断

b1=True

b2=True

if b1 and b2:

	print("both")

用if 而不是else控制

b=True
if b:
	print("yes")
else:
	print("no")
x=10
if x<5:
	print("s")
elif x<10:
	print("m")
elif x<15:
	print("l")
else:
	print("e")
c="a"
if(c>='A') and (c<='z'):
	print('u')
elif(c>='a') and (c<='z'):
	print('l')
else:
	print('n')

使用trick

x=42
if x>0:
	y=99

标签:cet4,cet6,return,else,abs,条件,print
From: https://www.cnblogs.com/aijingyn/p/17866148.html

相关文章

  • 99年的连接写法 on 只负责连接的条件 where 拼接其他的条件 临时表 自己拼自己
    --显示员工和上级领导员工表的领导编号=领导表的员工编号不能写反了selecte.empnoas'员工号',e.enameas'员工姓名',e.mgras'领导的员工号',l.enameas'领导的姓名'fromempeleftjoinemplone.mgr=l.empno;员工号|员工姓名|领导的员工号|领导的姓......
  • SQL HAVING 子句详解:在 GROUP BY 中更灵活的条件筛选
    SQLHAVING子句HAVING子句被添加到SQL中,因为WHERE关键字不能与聚合函数一起使用。HAVING语法SELECTcolumn_name(s)FROMtable_nameWHEREconditionGROUPBYcolumn_name(s)HAVINGconditionORDERBYcolumn_name(s);演示数据库以下是Northwind示例数据库中“Customers......
  • SQL HAVING 子句详解:在 GROUP BY 中更灵活的条件筛选
    SQLHAVING子句HAVING子句被添加到SQL中,因为WHERE关键字不能与聚合函数一起使用。HAVING语法SELECTcolumn_name(s)FROMtable_nameWHEREconditionGROUPBYcolumn_name(s)HAVINGconditionORDERBYcolumn_name(s);演示数据库以下是Northwind示例数据库中“Customers......
  • 聪明办法学python chap4:条件
    聪明办法学pythonchap4:条件if语句:通过缩进判断是不是在if里面ifelse:if成立else就不管了if不成立执行elsex=int(input())ifelifelse:平行依次判断:if():elif():elif():else:语法糖:(推导式)def(n):returnnif(n>=0)else-n等价于:def(n):​ ifn>=0:......
  • 11.26-task5-条件
    条件if语句if(condition):后面为condition为trueelse:后面为false布尔表达式的使用:我们知道当布尔值为true是返回值为1,false时返回值为0他的返回值意思是:检查n是否为负数,若为负数n<0为true=1n>=0为false=0,+前面就为1*-n,+后就为0,为正数时逻辑相同ifelse推导式......
  • python条件
    条件if语句if条件1:语句1elif条件2:语句2else:语句3if-els推导式ifexpression例:defabs7(n):returnnif(n>=0)else-nmatch-case语句match变量:case1:语句1case2:语句2case3:语句3case_:类似于default语句4case后的内容可以用|隔开,如1|2|3代码风格......
  • if条件控制
    一、if条件Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。可以通过下图来简单了解条件语句的执行过程:代码执行过程:二、if语句Python中if语句的一般形式如下所示:ifcondition_1:statement_block_1elifcondition_2:statem......
  • 【聪明办法学Python条件与判断】
    【聪明办法学Python条件与判断】if语句if-else推导式是Python中一种简洁而强大的语法,用于创建新的序列(如列表、集合、字典)的一种方法。Python中有三种主要的推导式:列表推导式、集合推导式和字典推导式。列表推导式:A=[list(map(int,input().split()))foriinrange(n)......
  • 条件语句和清晰代码风格
    if语句deff(x): print("A",end="") ifx==0:   print("B",end="")   print("C",end="") print("D")f(1)AD//defabs1(n): ifn<0:   n=-n returnn写在......
  • chap4 条件
    chap4条件1.if语句if():···<following_statement>如果输入正确则会执行程序,否则不会执行if语句里的内容ifelse语句```x=input("x=")x=float(x)print("hello")ifx<10:print("wahoo")else:print("ohno")print("goodbye&q......