Python指定任何非0和非空值为True,0 或者 None为False
Python 编程中if 语句用于控制程序的执行,基本形式为:
if 判断条件1: 执行语句1 elif 判断条件2: 执行语句2 else: 执行语句3 # 缩进不一致,会导致运行错误
代码示例:
score = 77 if score>=90 and score<=100: print('本次考试,等级为A') elif score>=80 and score<90: print('本次考试,等级为B') elif score>=70 and score<80: print('本次考试,等级为C') elif score>=60 and score<70: print('本次考试,等级为D') else: #elif可以else一起使用 print('本次考试,等级为E')
if嵌套代码示例
sex = 1 # 用1代表男生,0代表女生 solo = 1 # 用1代表单身,0代表有男/女朋友 if sex == 1: print("是男生") if solo == 1: print("我给你介绍一个吧?") else: print("你给我介绍一个呗?") else: print("你是女生") print("……")
注意:
外层的if判断,也可以是if-else
内层的if判断,也可以是if-else
根据实际开发的情况,进行选择
import random x=random.randint(0,2) #随机生成[0,2]的随机数 print(x)
语法格式:
for 临时变量 in 列表或者字符串等:
代码示例:
# 从[0,5)区间,从0开始,到5结束,默认步进值为1,取数 for i in range(5): print(i) # 循环输出变量名的值 name = 'chengdu' for x in name: print(x) # 从[0,12)区间,从0开始,到9结束,以步进值为3取值 for a in range(0,12,3): print(a) # 从数组中取出元素 a = ["aa","bb","cc","dd"] for i in range(len(a)): print(i,a[i])
语法格式:
while 表达式: #执行语句
代码示例:
i = 0 while i<5: print("当前是第%d次执行循环"%(i+1)) print("i=%d"%i) i+=1
语法格式
while 表达式: # 执行语句1 else: # 执行语句2
代码示例:
count = 0 while count<5: print(count,"小于5") count+=1 else: print(count,"大于或等于5")
i = 0 while i<6: i = i+1 print("-"*30) # 打印输出30个- if i==5: break print("当前i的值为%d"%i) print(i)
------------------------------ 当前i的值为1 ------------------------------ 当前i的值为2 ------------------------------ 当前i的值为3 ------------------------------ 当前i的值为4 ------------------------------ 5
i = 0 while i<6: i = i+1 print("-"*30) # 打印输出30个- if i==5: continue print("当前i的值为%d"%i) print(i)
------------------------------ 当前i的值为1 ------------------------------ 当前i的值为2 ------------------------------ 当前i的值为3 ------------------------------ 当前i的值为4 ------------------------------ ------------------------------ 当前i的值为6 6
请输入:剪刀(0)、石头(1)、布(2):
用户输入数字0-2中的一个数字,与系统随机生成的数字比较后给出结果信息
代码示例:
# 方式一: import random a = input("游戏参与者出招") print("游戏参与者输入的是: %s",a)1 b = random.randint(0,2) print(b) if a=="石头" or a=="剪刀" or a=="布": print("你输入的正确!") if a=="剪刀": a = 0 elif a=="石头": a = 1 elif a == "布": a = 2 if a>b: print("参与者胜") elif a=b: print("平局") else: print("机器人胜") else: print("你输入的有误,请按要求输入") # 方式二: import random person =int(input("please enter:[0:剪刀1:石头2:布] ")) computer = random.randint(0,2) print("电脑出拳: %d"%computer) if person > 2: print("enter wrong") elif person == 0 and computer ==2: print("good you win ") elif person == 1 and computer == 0: print("good you win ") elif person == 2 and computer == 1: print("good you win") elif person == computer : print("oh no 平手") else: print("your lose")
# 方式一: n= 100 a=1 sum = 0 while a<=n: print("第%d执行相加"%a) sum = sum + a a+=1 print("1到%d的和为:%d"%(n,sum)) # 方式二: i=1; n=101 sum = 0; for i in range(i,n,1): sum = sum + i print("%d到%d的和为:%d"%(i-99,n-1,sum))
# 方式一: a=1 while a< 9: a = a + 1 b = 1 while b<=a : print("%dx%d=%d"%(b,a,a*b),end=" ") b = b+1 print("",end="\n") # 方式二: for i in range(1,10): # print(i,end = ' ') for j in range(1,i+1): print('%s*%s=%s' %(i,j,i*j),end = ' ') print()