布尔类型;if语句;while循环;for循环

…1…

a=''
print(bool(a))                          # '' 的布尔类型为false

a=None                                  # None  什么都没 可以是任何类型
print(bool(a))                          # None 的布尔类型为false

a=2
print(bool(a))    

…2…

salary=int(input('请输入你的工资:'))
if salary>10000 and salary<20000:            # if 条件判读
    print('我要买迈腾')
elif salary>20000:                           # elif:else if 
    print('我要买宝马')
else:                                        # else 否则
    print('干嘛要买车呢,做地铁吧!')

90优秀80良好70普通60及格其他不及格

score=int(input('请输入你的成绩:'))
if score>=90 and score<=100:
    print('你的成绩是:优秀')
elif score<90 and score>=80:
    print('你的成绩是:良好')
elif score<80 and score>=70:
    print('你的成绩是:普通')
elif score<70 and score>=60:
    print('你的成绩是:及格')
elif score<60 and score>0:  # 如果用else 当输入超过100的数是输出的就会是不及格
    print('你的成绩是:不及格')

判段四级成绩

cet=int(input('请输入四级成绩:'))
if cet>=425 and cet<=700:
    print('恭喜成绩通过!')
else:
    cet<425
    print('请明年再战!')

if的使用

sex=input('请输入性别:')
if sex == '男':
    print('请领取剃须刀')
else:
    sex == '女'
    print('请领取化妆品')       

 money=int(input('请输入购物金额:'))
if money>5000:
    sex=input('请输入性别:')
    if sex=='男':
        print('赠送手表一块')
    elif sex=='女':
        print('赠送化妆品一套')
else:
    sex=input('请输入性别:')
    if sex=='男':
        print('赠送打火机一个')
    elif sex=='女':
        print('赠送手链一个')

买西瓜

price=float(input('请输入西瓜单价:'))
number=float(input('请输入斤数:'))
money=price*number
if money>300:
    vip=int(input('请输入vip级别:'))
    if vip==1:
        zh=money*0.8
        print('实付金额:',zh)
    elif vip==0:
        ml=int(money)-int(money)%10
else:
    print('赠送一个苹果付款金额:%.2f'%money)

…3…

while循环

money=int(input('请输入金额:'))
while money>=10:
    money-=10
    print('吃西瓜,剩下',money,'元')
print('钱不够了,不吃了,我的余额是:',money)

用while计算0~100的和

number=1
sum=0
while number<=100 :
    sum+=number
    number+=1
print(sum)

输出:

   5050

使用while计算30~50之间每隔三个数的乘积

i=50
cj=1
while i>=30:
    cj*=i
    i-=3
print(cj)

输出:

180428864000

使用while计算1~10之间的每隔三个数的乘积

i=10
j=1
while i>=0:
    j*=i
    i-=3
print(j)

输出:

280

#20-100能被3和7同时整除

i=20
while i<=100:
    i += 1
    while i%3==0 and i%7==0:
        print(i)
        i+=1

输出:

21
42
63
84

被3或7整除

i=20
while i<=100:
    if i%3==0 or i%7==0:
        print(i)
    i+=1

输出:

 21
24
27
28
30
33
35
36
39
42
45
48
49
51
54
56
57
60
63
66
69
70
72
75
77
78
81
84
87
90
91
93
96
98
99
for 循环
for i in range(20):
    print(i)

输出:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

用for计算1~100的和

sum=0
for i in range(1,101):
    sum+=i
print(sum)

输出:

5050

200-300之间,个位数+十位数=10

for i in range(200,301):
    gw=i%10
    sw=int(i/10%10)
    if gw+sw==10:
        print(i)

输出:

219
228
237
246
255
264
273
282
291

i=1
while i<=10:
    score=int(input('请输入第%d个人的成绩:'%i))
    if score<0:
        print('程序异常')
        break
    i+=1

输出:
*****
*****
*****
*****
*****

j=1
while j<=5:                   # while j<5 (j不包括5)
    i=1
    while i<=5:
        print('*',end='')
        i+=1
    print()
    j+=1

#判断一个数是不是质数,质数:只能被1和它本身整除

a=6
i=2
while i<a:
    if a%i==0:
        print(a,'不是质数')
        break
    i+=1
else:
    print(a,'是质数')

输出:

6 不是质数

输出九九乘法表

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	

line=1
while line<=9:
    colume=1
    while colume<=line:
        print('{} * {} = {}'.format(colume,line,colume*line),end='\t')
        #print(colume,'*',line,'=',colume*line,end='\t')
        #print('%d * %d = %d '%(colume,line,colume*line),end='\t')
        colume+=1
    print()
    line+=1

猜你喜欢

转载自blog.csdn.net/yue_lw/article/details/87869459