if条件控制语句
语法: if 条件按语句:
date = int(input('请输入日期:'))
if 0 < date < 11:
print('上旬')
elif 10 < date < 21:
print('中旬')
elif 20 < date < 32:
print('下旬')
else:
print('我也不知道什么期间')
input函数
它有阻塞程序
接受的参数都是字符串
#1.nput 语句有一个阻塞程序的功能,只有当用户输入了字符串后,代码才会继续执行
#2.input 输入的数据类型是字符串
int()可以讲其他的数据类型强制性转换为整型
a = int(input()) # 只有input语句输入了之后,后面的代码才会执行.
if else
# 语法:
# if 条件表达式 :
# 代码块
# else :
# 代码块
a = int(input('请输入0,1或者2的数字:'))
if a == 0: # True bool 0 True, 1, False
print('python')
else:
print('wrong')
if 永远都只会执行一行代码
If --elif–else
a = int(input('请输入0,1或者2的数字:'))
if a == 0: # True
print('I am great')
elif a == 3:
print('I am fine')
elif a == 4:
print('I am not bad')
else:
print('so-so')
# 匹配对应值的原则按执行顺序的条件被满足时程序执行结束。后面其他符合条件也不会执行。
value = 15000
if value >= 30000:
print('high salary')
elif value >= 20000 and value < 30000:
print('good salary')
elif value >= 5000 and value < 10000: # 5000 <= value < 10000:
print('average wage')
elif value >= 10000:
print('my salary is beyond')
elif value >= 2000:
print('I could feed myself only')
else:
print('my life is no insurance')
while循环嵌套
#Method 1
i = 0
while i < 5:
j = 0
while j < i+1:
print('*', end='')
j += 1
print()
i += 1
# Method2
i = 0
while i < 5:
i +=1
j = 0
while j < i:
print('*', end='')
j += 1
print()
"""
* i = i, j <1 j < i+1
** i = 1, j <2 j < i+1
*** i = 2, j < 3 j < i+1
**** i = 3, j < 4 j < i+1
***** i = 4, j < 5 j < i+1
"""
"""
***** i = 0 j < 5 i +j = 5 j = 5 - i
**** i = 1 j < 4
*** i = 2 j < 3
**
*
"""Method1
i = 0
while i < 5:
i += 1
j = 0
while j < 6-i:
print('*', end='')
j += 1
print()
"""Method2
i = 0
while i < 5:
j = 0
while j < 5-i:
print('*', end='')
j += 1
print()
i+=1
while循环
# while循环语法结构
# while + 条件表达式:代码块
# while 循环三条件:
# 设定初始值
# 条件判断
# 初始值要自增长
~~~求0-100 奇数与偶数和各是多少?
i = 0
even1=0
odd1=0
while i<100:
i +=1
if i%2 ==0:
even1 +=i
if i%2 != 0:
odd1 +=i
else:
print(f'0-100 偶数和={even1}\n0-100 奇数和={odd1}')
break和continue
内部循环的条件判断
break是终止循环
continue是跳过当次循环,继续执行下一次循环
i = 0
while i < 10:
if i == 3:
continue
print(i)
i += 1
else:
print('循环结束')
i = 0
while i < 10:
i += 1
if i == 3:
break
print(i)
else:
print('循环结束')
99 乘法表
#99乘法表(正序) while 法
i = 0
while i < 9:
i += 1
j = 0
while j < i:
j += 1
print(f'{j}*{i}={i*j}',end='\t')
print()
# 2. for in 遍历法由低到高
#99乘法表(正序) for in 循环 从小到大法
# 使用递增
for i in range(0,9):
i +=1 # 使用递增
for c in range(i):
c += 1
print(f'{c}*{i}={c*i}',end=' ')
print()
# 不使用递增
for i in range(1,10):# 不使用递增
for c in range(i):
c += 1
print(f'{c}*{i}={c*i}',end=' ')
print()
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
#99乘法表(降序) while 法
i=10
while 0<i<=10:
i-=1
j=0
while j<i:
j+=1
print(f'{j}*{i}={j*i}',end='\t')
print()
else:
pass
#99乘法表(倒序) for in 循环 从大到小法遍历数据
# 使用递减
for i in range(10,0,-1): #使用从大到小法遍历数据
i -=1 # 使用递减
for c in range(i):
c += 1
print(f'{c}*{i}={c*i}',end=' ')
print()
# 不使用递减
for i in range(9,0,-1): # 不使用递减
for c in range(i):
c += 1
print(f'{c}*{i}={c*i}',end=' ')
print()
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*4=4 2*4=8 3*4=12 4*4=16
1*3=3 2*3=6 3*3=9
1*2=2 2*2=4
1*1=1
#99乘法表(倒序法) for in 循环 从大到小法
for i in range(9,0,-1):
for c in range(i,0,-1):
print(f'{c}*{i}={c*i}',end=' ')
print()
9*9=81 8*9=72 7*9=63 6*9=54 5*9=45 4*9=36 3*9=27 2*9=18 1*9=9
8*8=64 7*8=56 6*8=48 5*8=40 4*8=32 3*8=24 2*8=16 1*8=8
7*7=49 6*7=42 5*7=35 4*7=28 3*7=21 2*7=14 1*7=7
6*6=36 5*6=30 4*6=24 3*6=18 2*6=12 1*6=6
5*5=25 4*5=20 3*5=15 2*5=10 1*5=5
4*4=16 3*4=12 2*4=8 1*4=4
3*3=9 2*3=6 1*3=3
2*2=4 1*2=2
1*1=1
#99乘法表(倒序法) for in 循环 从大到小法2
for i in range(1,10,+1):
for c in range(i,0,-1):
print(f'{c}*{i}={c*i}',end=' ')
print()
1*1=1
2*2=4 1*2=2
3*3=9 2*3=6 1*3=3
4*4=16 3*4=12 2*4=8 1*4=4
5*5=25 4*5=20 3*5=15 2*5=10 1*5=5
6*6=36 5*6=30 4*6=24 3*6=18 2*6=12 1*6=6
7*7=49 6*7=42 5*7=35 4*7=28 3*7=21 2*7=14 1*7=7
8*8=64 7*8=56 6*8=48 5*8=40 4*8=32 3*8=24 2*8=16 1*8=8
9*9=81 8*9=72 7*9=63 6*9=54 5*9=45 4*9=36 3*9=27 2*9=18 1*9=9
Homework 5
# 列出1000以内的水仙花数 (阿姆斯状数),并求和。
i = 100
r=0
while i<1000:
a=i//100
b=i//10%10
c=i%10
if a**3+b**3+c**3==i:
r+=i
print(i)
i +=1
else:
print(r)
***
153
370
371
407
1301
列出1000-10000以内的水仙花数 (阿姆斯状数)
i = 1000
r=0
while i<9999:
i += 1
a=i//1000
b=i//100%10
c=i//10%10
d=i%10
if a**4+b**4+c**4+d**4==i:
print(i)
else:
pass
1634
8208
9474
# 获取用户输入的任意数,判断其是否是质数?
**Method 1
num=int(input("please input a num"))
i=2
while i<num:
if num%i==0:
print("it is not prime number")
break
i +=1
else:
print("it is prime number")
***Metnod2
num=int(input("please input a num"))
i=1
while i<num:
i +=1
if num%i==0:
print("it is not prime number")
break
else:
print("it is prime number")
# 3. 猜拳游戏:
# • 出拳 玩家:手动输入 电脑:随机输入
# • 判断输赢: 玩家获胜 电脑获胜 平
while True:
import random
player = int(input('请输入:'))
list1 = ['拳头','剪刀','布']
random.choice(list1)
computer = random.randint(0, 2)
if (player == 1 and computer == 2) or (player == 0 and computer == 1) or (player == 2 and computer == 0):
print('玩家获胜')
elif player == computer:
print('平局')
else:
print('庄家获胜')