首页 > 其他分享 >5.判断和循环

5.判断和循环

时间:2024-09-02 11:03:52浏览次数:13  
标签:guess 判断 level 18 please 循环 print sorry

判断和循环

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 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=' ')
        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='')
  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)*"*"}')
    *
   ***
  *****
 *******
*********

标签:guess,判断,level,18,please,循环,print,sorry
From: https://www.cnblogs.com/csfy0524/p/18392333

相关文章

  • Python控制流:循环语句(for, while)②
    文章目录前言1.`for`循环1.1遍历列表1.2遍历字符串1.3使用`range()`函数1.4`enumerate()`函数1.5`zip()`函数2.`while`循环2.1基本用法2.2无限循环2.3使用`break`和`continue`语句3.嵌套循环4.常见错误和最佳实践4.1常见错误4.2最佳实践5.......
  • Prop效验与Prop默认值用法及循环遍历数组
    Prop效验与使用在HBuilderX里面你把组件传过去,向之前的那样的写法是没有默认值的,写了才有值,否则为空,所以我们可以用另一种方法,写法如下虽然这样写了但是不是完全体的,我们可以给他定个默认值和类型,就像那个String一样,可以约束对象只能是这个的类型这样子另一个页面......
  • 各位天命人!国自然评审意见出来了,那如何判断是否上会?
    公众号:生信漫谈,获取最新科研信息!各位天命人!国自然评审意见出来了,那如何判断是否上会?https://mp.weixin.qq.com/s?__biz=MzkwNjQyNTUwMw==&mid=2247487055&idx=1&sn=1dc8b66e10323d37e477e8829dad0c76&chksm=c0e9ebbff79e62a94fcd49341ae18cfe19aa3aa8ba339bed8ff329a080022dc7......
  • 数据结构 栈与队列 --循环队列
    栈和队列--循环队列文章目录栈和队列--循环队列前言一、队列是什么?1.2时间复杂度比较1.3难题及解决二、队列的实现2.1循环队列结构体实现2.2循环队列基本操作总结前言前情提要:之前的文章讲了队列和栈的基本概念,今天来讲队列的具体实现一、队列是什么?队......
  • [Java手撕]循环打印ABC
    多线程循环打印ABCimportjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;publicclassMain{publicstaticReentrantLockLock=newReentrantLock();publicstaticConditionConditionA=Lock.newCondition();......
  • computed计算属性及方法对比和循环遍历统计以及watch和watchEect监听的用法
    1.computed计算属性及方法对比1.了解computed计算属性和用法在我们的一些应用中可以看的应用会给我们提供一些计算类的功能比如取名,它会给你提供两个输入框,然后在你给这两个输入框输入值的时候会在下方生成你输入这个两个值的结合值,就比如你先输入了一个姓氏,然后输入一个名,下......
  • 【网络基础知识】判断证书私钥格式
    原创小何运维笔记要判断证书私钥的格式,可以采取以下步骤:查看文件扩展名:文件扩展名可以提供一些线索:.pem:PEM格式,通常是Base64编码的,可以包含多种类型的密钥或证书。.key:通常是私钥文件,可能是PEM或其他格式。.der:DER格式,通常是二进制编码,不易用文本编辑器直接查看。.pfx......
  • 三、分支和循环
    一、if语句1.1if语法形式:if(表达式)语句;表达式成立为真,则语句执行。表达式不成立为假,语句不执行。注:c语言中,0为假,非0为真。1.2else语法形式:if(表达式)语句1;else语句2;1.3分支中包含多条语句在if和else语句中,默认都只能控制一条语句。如果i......
  • 循环
    Java有三种循环:for,while,dowhile。for基本语法:for(循环变量初始化;循环条件;循环变量迭代){循环语句;}程序示例:publicstaticvoidmain(String[]args){for(inti=0,j=0;i<3;i++,j--){System.out.println("HelloWorld");}}......
  • c语言分支与循环详解
    使用if、switch实现分支结构,使用for、while、dowhile实现循环结构分支:1.1if语句的语法if(表达式) 语句;在c语言中0表示假,则语句不执行。非0表示真,语句执行1.2else与if对应,比如说一个数不是奇数就是偶数了if(表达式) 语句1;else 语句2;表达式成立则执行语句1,不成......