选择结构

关键字和保留字
在python中,系统内置了一些具有特殊含义的单词或者词组,关键字或者保留字
如何查看python关键字

import keyword

 dir(keyword)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', 'iskeyword', 'kwlist', 'main']

keyword.kwlist  # 返回一个列表

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
 

冯诺依曼提出

  • 顺序 : 代码都是从左而右,从上到下执行的
  • 选择结构
    1. 单分支
if条件:
	 #条件成立后

age=input("请输入你的年龄")
age=int(age)
# 单分支,当年龄大于等于18岁,告诉他你成年了
# 当提交成立,代码就会进入if控制的语句块中去
if age>=18:
	print("你已经成年了,可以干一些羞羞的事了")
	print("骗你的,你还真信")	
  1. 双分支
if condition:
		# 条件成立的执行代码
else:
		# 条件不成立的需要执行的代码

age=int(input("请输入你的年龄:"))
if age >=18:
	print("你已经成年了,嘿嘿")
else:
	print("你还未成年,回家找妈妈~~,乖")
  1. 三分支
if condition1:
		#符合条件1,则执行代码
elif condition2:
		#符合条件2,则执行代码
elif condition3:
		#符合条件3,则执行代码
elif conditionn:
		#符合条件n,则执行代码
	···
实例:如果没有多分支的话

score=float (input("请输入你的成绩:"))
if score >100 or score <0:
	print("你是火星来的吗?")
else :
	#合法成绩
	if score == 0 :
		print("0分")	
	else :
		if score >0 and score <60:
			print("不合格")
		else:
			if score >=60 and score<80:
				print("合格")
			else:
				if score >=80 and score <90:
					print("良好")
				else:
					if score >=90 and score<100:
						print("优秀")
					else :
						print("满分")
这样写很繁琐,所以采用三分支如下:

week = (input("请输入今天的星期数:"))
if week =="1":
	print("今天星期一,猴子穿花衣")
elif week == "2":
	print("今天星期二,猴子去爬山")
elif week == "3":
	print("今天星期三,猴子爬大山")
elif week == "4":
	print("今天星期四,猴子去跑步")
elif week == "5":
	print("今天星期五,猴子去跳舞")
elif week == "6":
	print("今天星期六,猴子摘石榴")
elif week == "7":
	print("今天星期日,猴子过生日")
else:
	print("对比起,你是猴子派来的逗逼吗?")

题外话:
注意:python是强制缩进的语言

	if(age>18){
		print("hh")
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值