西蒙少 发表于 2024-9-2 13:11:25

5.判断和循环

判断和循环

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.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 older1.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 older2 循环

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+=1please 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: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 doll2.2 for循环

其实和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}')
                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 doll2.2.1 for+break的方法

# 数据实例引用水导的知识点,我只是在慢慢学习啦
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']

for i in game_list:
    print(i)xiaoxiaokan
kaixinxiaoxiaole
tiaoyitiao
chaojimali
hundouluo
zhizhuzhipai
saolei
renzheshengui2.2.2 for+continue

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

for i in game_list:
    if i == 'tiaoyitiao':
      break
    print(i)xiaoxiaokan
kaixinxiaoxiaole
tiaoyitiao
chaojimali
hundouluo
zhizhuzhipai
saolei
renzheshenguichaojimalihundouluozhizhuzhipaisaoleirenzheshengui2.2.3 for循环的嵌套

这个真的代码里面最常用,但是底层逻辑相对而言不是那么难
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
renzheshengui2.2.4 for+else

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

for i in game_list:
    if i == 'tiaoyitiao':
      continue
    print(i)else:    print('print finish')xiaoxiaokan
kaixinxiaoxiaole
chaojimali
hundouluo
zhizhuzhipai
saolei
renzheshenguiprint finish3 作业

3.1 输入年龄进行判断

for i in range(3):
    for j in range(3):
      if i*j !=0:
            print(i*j)
else:
    print('print finish')age = int(input('请输入年龄:'))if age150:    print('不属于年龄范围内的数字')else:    if age
页: [1]
查看完整版本: 5.判断和循环