翼度科技»论坛 编程开发 python 查看内容

5.判断和循环

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
判断和循环

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 判断的三种形式
  1. 1.if
  2. 2.if……else
  3. 3.if……elif……else
复制代码
  1. #直接if的语句
  2. real_name ='xiaocao'
  3. name = input('please enter the name:')
  4. if name == real_name:
  5.     print('congratulations!')
复制代码
  1. please enter the name:xiaocao
  2. congratulations!
复制代码
  1. #if……else的语句
  2. real_name ='xiaocao'
  3. name = input('please enter the name:')
  4. if name == real_name:
  5.     print('congratulations!')
  6. else:
  7.     print('what a pity! Guess wrong')
复制代码
  1. please enter the name:xiaofang
  2. what a pity! Guess wrong
复制代码
  1. # if……elif……else
  2. real_age = 18
  3. age = int(input("please enter the age"))
  4. if age <18:
  5.     print("sorry,guess younger")
  6. elif age >18:
  7.     print("sorry,guess older")
  8. else:
  9.     print('congratulations!')
复制代码
  1. please enter the age25
  2. sorry,guess older
复制代码
1.2 判断语句的嵌套

说白了就是一个if语句中又嵌套了一个if语句
  1. # if……elif……else
  2. real_age = 18
  3. age = int(input("please enter the age"))
  4. if age != 18:
  5.     print("sorry,guess wrong")
  6.     if age >18:
  7.         print("sorry,guess older")
  8.     else:
  9.         print("sorry,guess younger")
  10. else:
  11.     print('congratulations!')
复制代码
  1. please enter the age25
  2. sorry,guess wrong
  3. sorry,guess older
复制代码
2 循环

2.1 while循环

在循环中有两个比较重要的函数:
break和continue
break就是直接回到开始的地方
continue就是跳出这一步,后面继续
  1. #while的循环好处就是可以直接将所需要的判断条件作为一个真实值增加break进行判断
  2. real_age = 18
  3. while True:
  4.     age = int(input("please enter the age"))
  5.     if age == 18:
  6.         print('congratulations!')
  7.         break
  8.     else:
  9.         print("sorry,guess wrong")
  10.         if age >18:
  11.             print("sorry,guess older")
  12.         else:
  13.             print("sorry,guess younger")
  14.         
复制代码
  1. please enter the age23
  2. sorry,guess wrong
  3. sorry,guess older
  4. please enter the age15
  5. sorry,guess wrong
  6. sorry,guess younger
  7. please enter the age18
  8. congratulations!
复制代码
  1. #循环中的continue的用法
  2. num = 0
  3. while num <=15:
  4.     if num == 10:
  5.         num +=1
  6.         continue
  7.     else:
  8.         print(num)
  9.     num+=1
复制代码
  1. please enter the age23
  2. sorry,guess wrong
  3. sorry,guess older
  4. please enter the age15
  5. sorry,guess wrong
  6. sorry,guess younger
  7. please enter the age18
  8. congratulations!please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}please enter the num:2Sorry, we don't have this gift.Please reselectplease choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}please enter the num:1Congratulations, you have received this gift,the gift is doll
复制代码
2.2 for循环

其实和while差不多,好处就是他不会溢出,知道位置便可以打印了
这个循环函数可以做一些效果出来
  1. # 注意,这里有几个循环终止的时候就需要几个break
  2. real_age = 18
  3. while True:
  4.     age = int(input("please enter the age"))
  5.     if age == 18:
  6.         print('congratulations!')
  7.         while True:
  8.             prize_dict = {0:'toy_car',1:'doll',2:'puzzle'}
  9.             print(f'please choose one of these gifts: {prize_dict}')
  10.             prize = int(input('please enter the num:'))
  11.             if prize == 1:
  12.                 print(f'Congratulations, you have received this gift,the gift is {prize_dict[1]}')
  13.                 break
  14.             else:
  15.                 print("Sorry, we don't have this gift.Please reselect")
  16.         break
  17.                
  18.     else:
  19.         print("sorry,guess wrong")
  20.         if age >18:
  21.             print("sorry,guess older")
  22.         else:
  23.             print("sorry,guess younger")
复制代码
  1. please enter the age23
  2. sorry,guess wrong
  3. sorry,guess older
  4. please enter the age15
  5. sorry,guess wrong
  6. sorry,guess younger
  7. please enter the age18
  8. congratulations!
  9. please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
  10. please enter the num:2
  11. Sorry, we don't have this gift.Please reselect
  12. please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
  13. please enter the num:1
  14. Congratulations, you have received this gift,the gift is doll
复制代码
2.2.1 for+break的方法
  1. # 数据实例引用水导的知识点,我只是在慢慢学习啦
  2. game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
  3. for i in game_list:
  4.     print(i)
复制代码
  1. xiaoxiaokan
  2. kaixinxiaoxiaole
  3. tiaoyitiao
  4. chaojimali
  5. hundouluo
  6. zhizhuzhipai
  7. saolei
  8. renzheshengui
复制代码
2.2.2 for+continue
  1. game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
  2. for i in game_list:
  3.     if i == 'tiaoyitiao':
  4.         break
  5.     print(i)
复制代码
  1. xiaoxiaokan
  2. kaixinxiaoxiaole
  3. tiaoyitiao
  4. chaojimali
  5. hundouluo
  6. zhizhuzhipai
  7. saolei
  8. renzheshenguichaojimalihundouluozhizhuzhipaisaoleirenzheshengui
复制代码
2.2.3 for循环的嵌套

这个真的代码里面最常用,但是底层逻辑相对而言不是那么难
  1. game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
  2. for i in game_list:
  3.     if i == 'tiaoyitiao':
  4.         continue
  5.     print(i)
复制代码
  1. xiaoxiaokan
  2. kaixinxiaoxiaole
  3. chaojimali
  4. hundouluo
  5. zhizhuzhipai
  6. saolei
  7. renzheshengui
复制代码
2.2.4 for+else
  1. game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
  2. for i in game_list:
  3.     if i == 'tiaoyitiao':
  4.         continue
  5.     print(i)else:    print('print finish')
复制代码
  1. xiaoxiaokan
  2. kaixinxiaoxiaole
  3. chaojimali
  4. hundouluo
  5. zhizhuzhipai
  6. saolei
  7. renzheshenguiprint finish
复制代码
3 作业

3.1 输入年龄进行判断
  1. for i in range(3):
  2.     for j in range(3):
  3.         if i*j !=0:
  4.             print(i*j)
  5. else:
  6.     print('print finish')
复制代码
[code]age = int(input('请输入年龄:'))if age150:    print('不属于年龄范围内的数字')else:    if age

举报 回复 使用道具