5.判断和循环

cnblogs 2024-09-02 11:09:00 阅读 88

怎么说呢,这个可能有点儿格式问题,因为jupyter写完以后,有几个代码后面几天补的,导致其不能正常运行,呜呜呜呜呜,会哭亖,然后新建一个运行完了保存的

判断和循环

1 判断

1.1 判断的三种形式

1.2 判断的嵌套

2 循环

2.1 while循环

2.2 for循环

3 作业

3.1 输入年龄进行判断

3.2 打印偶数

3.3 猜年龄游戏

3.4 9*9乘法表

3.5 金字塔的打印

1 判断

1.1 判断的三种形式

<code>1.if

2.if……else

3.if……elif……else

#直接if的语句

real_name ='xiaocao'

name = input('please enter the name:')

if name == real_name:

print('congratulations!')

please enter the name:xiaocao

congratulations!

#if……else的语句

real_name ='xiaocao'

name = input('please enter the name:')

if name == real_name:

print('congratulations!')

else:

print('what a pity! Guess wrong')

please enter the name:xiaofang

what a pity! Guess wrong

# if……elif……else

real_age = 18

age = int(input("please enter the age"))

if age <18:

print("sorry,guess younger")

elif age >18:

print("sorry,guess older")

else:

print('congratulations!')

please enter the age25

sorry,guess older

1.2 判断语句的嵌套

说白了就是一个if语句中又嵌套了一个if语句

# if……elif……else

real_age = 18

age = int(input("please enter the age"))

if age != 18:

print("sorry,guess wrong")

if age >18:

print("sorry,guess older")

else:

print("sorry,guess younger")

else:

print('congratulations!')

please enter the age25

sorry,guess wrong

sorry,guess older

2 循环

2.1 while循环

在循环中有两个比较重要的函数:

break和continue

break就是直接回到开始的地方

continue就是跳出这一步,后面继续

#while的循环好处就是可以直接将所需要的判断条件作为一个真实值增加break进行判断

real_age = 18

while True:

age = int(input("please enter the age"))

if age == 18:

print('congratulations!')

break

else:

print("sorry,guess wrong")

if age >18:

print("sorry,guess older")

else:

print("sorry,guess younger")

please enter the age23

sorry,guess wrong

sorry,guess older

please enter the age15

sorry,guess wrong

sorry,guess younger

please enter the age18

congratulations!

#循环中的continue的用法

num = 0

while num <=15:

if num == 10:

num +=1

continue

else:

print(num)

num+=1

0

1

2

3

4

5

6

7

8

9

11

12

13

14

15

2.1.1 while的嵌套

就是在一个while语句里面包含了另一个的while语句

# 注意,这里有几个循环终止的时候就需要几个break

real_age = 18

while True:

age = int(input("please enter the age"))

if age == 18:

print('congratulations!')

while True:

prize_dict = {0:'toy_car',1:'doll',2:'puzzle'}

print(f'please choose one of these gifts: {prize_dict}')

prize = int(input('please enter the num:'))

if prize == 1:

print(f'Congratulations, you have received this gift,the gift is {prize_dict[1]}')

break

else:

print("Sorry, we don't have this gift.Please reselect")

break

else:

print("sorry,guess wrong")

if age >18:

print("sorry,guess older")

else:

print("sorry,guess younger")

please enter the age23

sorry,guess wrong

sorry,guess older

please enter the age15

sorry,guess wrong

sorry,guess younger

please enter the age18

congratulations!

please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}

please enter the num:2

Sorry, we don't have this gift.Please reselect

please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}

please enter the num:1

Congratulations, you have received this gift,the gift is doll

2.2 for循环

其实和while差不多,好处就是他不会溢出,知道位置便可以打印了

这个循环函数可以做一些效果出来

# 数据实例引用水导的知识点,我只是在慢慢学习啦

game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']

for i in game_list:

print(i)

xiaoxiaokan

kaixinxiaoxiaole

tiaoyitiao

chaojimali

hundouluo

zhizhuzhipai

saolei

renzheshengui

2.2.1 for+break的方法

game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']

for i in game_list:

if i == 'tiaoyitiao':

break

print(i)

xiaoxiaokan

kaixinxiaoxiaole

2.2.2 for+continue

game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']

for i in game_list:

if i == 'tiaoyitiao':

continue

print(i)

xiaoxiaokan

kaixinxiaoxiaole

chaojimali

hundouluo

zhizhuzhipai

saolei

renzheshengui

2.2.3 for循环的嵌套

这个真的代码里面最常用,但是底层逻辑相对而言不是那么难

for i in range(3):

for j in range(3):

if i*j !=0:

print(i*j)

1

2

2

4

2.2.4 for+else

for i in range(3):

for j in range(3):

if i*j !=0:

print(i*j)

else:

print('print finish')

1

2

2

4

print finish

3 作业

3.1 输入年龄进行判断

输入姑娘的年龄后,进行以下判断:

1. 如果年龄小于18岁,打印“未成年”

2. 如果年龄大于18岁小于40岁,打印“青年人”

3. 如果年龄大于40岁小于60岁,打印“中年人”

4. 如果年龄大于60岁,打印“老年人”

age = int(input('请输入年龄:'))

if age<0 or age >150:

print('不属于年龄范围内的数字')

else:

if age <= 18:

print('未成年')

elif age <=40:

print('青年人')

elif age <= 60:

print('中年人')

else:

print('老年人')

请输入年龄:65

老年人

3.2 打印偶数

打印1-100之间的偶数

# fun 1

for i in range(1,101):

if i %2 == 0:

print(i,end = ' ')

i+=1

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

# fun 2

#这个代码多少有些麻烦,改一个版本

num = 1

while num <=100:

if num % 2 == 1:

num+=1

continue

else:

print(num,end =' ')

num+=1

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

num = 1

while num<=100:

if num % 2 == 0 :

print(num,end =' ')

num+=1

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

3.3 猜年龄游戏

猜年龄游戏升级版,有以下三点要求:

1. 允许用户最多尝试3次

2. 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序

3. 如果猜对了,就直接退出

real_age = 18

chance = 1

while True:

if chance <=3:

age = int(input("please enter the age"))

if age == 18:

print('congratulations!')

break

else:

print("sorry,guess wrong")

if age >18:

print("sorry,guess older")

else:

print("sorry,guess younger")

elif chance == 4:

choice = input('please tell me to continue<<<')

if choice == 'y'or choice =='Y':

chance = 1

continue

else:

break

chance +=1

please enter the age23

sorry,guess wrong

sorry,guess older

please enter the age14

sorry,guess wrong

sorry,guess younger

please enter the age15

sorry,guess wrong

sorry,guess younger

please tell me to continue<<<y

please enter the age14

sorry,guess wrong

sorry,guess younger

please enter the age16

sorry,guess wrong

sorry,guess younger

please enter the age17

sorry,guess wrong

sorry,guess younger

please tell me to continue<<<n

3.4 打印乘法表

for i in range(1,10):

for j in range(1,10):

if i > j:

print(f'{j}*{i}={i*j}',end=' ')code>

elif i == j:

print(f'{j}*{i}={i*j}',end = '\n')

else:

continue

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

# 上面的代码,貌似有些麻烦,看了别人的觉得我的可以更改一下

for i in range(1,10):

for j in range(1,i+1):

print(f'{i}*{j}={i*j}',end =' ')

print()

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

3.5 打印金字塔

'''

# max_level=5

* # current_level=1,空格数=4,*号数=1

*** # current_level=2,空格数=3,*号数=3

***** # current_level=3,空格数=2,*号数=5

******* # current_level=4,空格数=1,*号数=7

********* # current_level=5,空格数=0,*号数=9

# 数学表达式

空格数 = max_level-current_level

*号数 = 2*current_level-1

'''

max_level=int(input("请输入最大层数"))

for i in range(max_level):

print(f'{" "*(max_level-i-1)}',end='')code>

print(f'{"*"*(2*(i+1)-1)}')

请输入最大层数5

*

***

*****

*******

*********

max_level = 5

for current_level in range(1,max_level+1):

print(f'{(max_level-current_level)*" "}{(2*current_level-1)*"*"}')

*

***

*****

*******

*********

max_level = 5

for current_level in range(1,max_level+1):

space_num = max_level-current_level

star_num = 2*current_level-1

print(f'{(space_num)*" "}{(star_num)*"*"}')

*

***

*****

*******

*********


上一篇: Python NumPy 库详解

下一篇: Java之内部类

本文标签

python学习   


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。