流程控制之判断,while循环,for循环

流程控制之判断

# 语法1# if 条件:#     代码1#     代码2#     代码3#     ...

# cls=‘human‘# sex=‘female‘# age=18## if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:#     print(‘开始表白‘)## print(‘end....‘)##

# 语法2# if 条件:#     代码1#     代码2#     代码3#     ...# else:#     代码1#     代码2#     代码3#     ...

# cls=‘human‘# sex=‘female‘# age=38## if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:#     print(‘开始表白‘)# else:#     print(‘阿姨好‘)## print(‘end....‘)

# 语法3# if 条件1:#     代码1#     代码2#     代码3#     ...# elif 条件2:#     代码1#     代码2#     代码3#     ...# elif 条件3:#     代码1#     代码2#     代码3#     ...# ............# else:#     代码1#     代码2#     代码3#     ...

‘‘‘如果:成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

‘‘‘

# score=input(‘your score: ‘) #score=‘73‘# score=int(score) #score=73# if score >= 90:#     print(‘优秀‘)# elif score >= 80:#     print(‘良好‘)# elif score >= 70:#     print(‘普通‘)# else:#     print(‘很差‘)

# user_from_db=‘egon‘# pwd_from_db=‘123‘## user_from_inp=input(‘username>>>: ‘)# pwd_from_inp=input(‘password>>>: ‘)## if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:#     print(‘login successfull‘)# else:#     print(‘user or password error‘)

#if的嵌套

cls=‘human‘sex=‘female‘age=18is_success=False

if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:    print(‘开始表白...‘)    if is_success:        print(‘在一起‘)    else:        print(‘我逗你玩呢....‘)else:    print(‘阿姨好‘)

print(‘end....‘)

流程控制之while循环

#while语法,while循环又称为条件循环# while 条件:#     code1#     code2#     code3#     ....

# user_db=‘egon‘# pwd_db=‘123‘## while True:#     inp_user=input(‘username>>: ‘)#     inp_pwd=input(‘password>>: ‘)#     if inp_user == user_db and inp_pwd == pwd_db:#         print(‘login successfull‘)#     else:#         print(‘user or password error‘)

#2 while+break:break的意思是终止掉当前层的循环,.执行其他代码# while True:#     print(‘1‘)#     print(‘2‘)#     break#     print(‘3‘)

# user_db=‘egon‘# pwd_db=‘123‘## while True:#     inp_user=input(‘username>>: ‘)#     inp_pwd=input(‘password>>: ‘)#     if inp_user == user_db and inp_pwd == pwd_db:#         print(‘login successfull‘)#         break#     else:#         print(‘user or password error‘)

# print(‘其他代码‘)

#3 while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环#ps:记住continue一定不要加到循环体最后一步执行的代码# n=1# while n <= 10: ##     if n == 8:#         n += 1 #n=9#         continue#     print(n)#     n+=1 #n=11

# while True:#     if 条件1:#         code1#         code2#         code3#         continue #无意义#     elif 条件1:#         code1#         continue #有意义#         code2#         code3#     elif 条件1:#         code1#         code2#         code3#         continue #无意义#     ....#     else:#         code1#         code2#         code3#         continue #无意义

#while循环嵌套user_db=‘egon‘pwd_db=‘123‘

while True:    inp_user=input(‘username>>: ‘)    inp_pwd=input(‘password>>: ‘)    if inp_user == user_db and inp_pwd == pwd_db:        print(‘login successfull‘)        while True:            cmd=input(‘请输入你要执行的命令: ‘)            if cmd == ‘q‘:                break            print(‘%s 功能执行...‘ %cmd)        break    else:        print(‘user or password error‘)

print(‘end....‘)

#while+taguser_db=‘egon‘pwd_db=‘123‘

tag=Truewhile tag:    inp_user=input(‘username>>: ‘)    inp_pwd=input(‘password>>: ‘)    if inp_user == user_db and inp_pwd == pwd_db:        print(‘login successfull‘)        while tag:            cmd=input(‘请输入你要执行的命令: ‘)            if cmd == ‘q‘:                tag=False            else:                print(‘%s 功能执行...‘ %cmd)

else:        print(‘user or password error‘)

print(‘end....‘)

#while+else (***)n=1while n < 5:    # if n == 3:    #     break    print(n)    n+=1else:    print(‘在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码‘)

流程控制之for循环

# names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]

# n=0# while n < len(names):#     print(names[n])#     n+=1

# names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]# info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}## # for k in info: #x=‘‘age‘# #     print(k,info[k])## for item in names:#     print(item)

# for i in range(1,10):#     print(i)

# for i in range(10): #默认的起始位置是0#     print(i)

# for i in range(1,10,2): #1 3  5  7  9#     print(i)

# names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]# for i in range(len(names)):#     print(i,names[i])

# for i in range(5):#     print(‘========>第一层: %s<=========‘ %i)#     for j in range(3):#         print(‘          第二层: %s‘ %j)

#for+break# names=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]# for n in names:#     if n == ‘egon‘:#         break#     print(n)

#for+continue# names=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]# for n in names:#     if n == ‘egon‘:#         continue#     print(n)

#for+elsenames=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]for n in names:    # if n == ‘egon‘:    #     break    print(n)else:    print(‘=====>‘)


原文地址:https://www.cnblogs.com/chillwave/p/9108540.html

时间: 2024-07-29 07:05:34

流程控制之判断,while循环,for循环的相关文章

4 流程控制》4.5 比较for循环和while循环

4.5.2 计算用户输入的数字的总和 下面的程序让用户输入一些数字,然后打印出这些数字的总和. ① 这是使用for循环的版本: # forsum.pyn = int(input('How many numbers to sum?'))total = 0for i in range(n):        s = input('Enter number ' + str(i + 1) + ':')        total = total + int(s)print('The sum is ' + s

python流程控制-条件语句If,while循环

一.If,条件语句-选择 格式:python简洁优美,注意缩进 1.第一种: if 条件: 四个空格(tab键)  满足条件时的执行步骤 if 5>4 : print(666) print(777) 2.第二种: if 条件: 四个空格(tab键)  满足条件时的执行步骤 else: 四个空格(tab键)  不满足条件时的执行步骤 3.第三种(多选): if 条件1: 四个空格(tab键)  满足条件1时的执行步骤 elif 条件2: 四个空格(tab键)  满足条件2时的执行步骤 ......

023 程序流程控制while判断

while循环 比如我们人每天早上都要起床,刷牙洗脸,这是我们每天都是要做的,这就代表我们每天都是会做同样的事情,这叫做一种循环.那我们计算机有时候也会需要做一些重复的事情,那理所应当的我们计算机也需要用到循环.那这就讲到了while循环,while循环他又称为条件循环. 1.语法 while(当) <条件>: <需要进行重复的代码块> # 当条件成立时会进行运行,结束完代码块后会再一次判断条件,成立再运行,运行完再判断条件 # 实现猜年龄的功能 age = 16 while Tr

第二节 java流程控制(判断结构+选择结构)

Java的判断结构: 1.if(条件表达式){ 执行语句 }: 2.if(条件表达式){ 执行语句 }else{ 执行语句 } 3. if(条件表达式){ 执行语句 }else if(条件表达式){ 执行语句 } else{ 执行语句 } 4.嵌套的if...else语句 if(条件表达式){ 执行语句1 if(条件表达式){ 执行语句2 }else{ 执行语句3 } } switch语句  区间判断选择 switch(表达式){ case 取值1:执行语句1;break         //

流程控制

什么是流程控制? js中的语句一般是按照写的顺序来运行的(从上到下,从左至右).这种运行称为顺序运行,是程序流的默认方向. 与顺序运行不同,另一种运行将程序流转换到脚本的另外的部分.也就是不按顺序运行下一条语句,而是运行另外的语句.这种运行方式就叫做流程控制.简单来讲流程控制就是根据不同的情况做不同的事情.常见的流程控制有if语句.for循环.while循环. if语句 js支持if和if...else条件语句.在if语句中将测试一个条件,如果该条件满足测试,执行相关的js代码.在 if...e

Swift的流程控制和函数

流程控制(Control Flow) For循环 for循环和C语言,OC用法很相像,通常有for()和 for in两种. 遍历时可以利用下划线"_"忽略对值的访问等. Switch switch在Swift中则显得灵活的多,不过需要注意的是,不同于C语言,Swift中的Switch不存在隐式的贯穿,每个case里的表达式后面不需要写break则会自动跳出. 如果你想保留这次匹配并向下匹配的话,请使用fallthrough关键字. 每个case里必须要有表达式,每个switch也必须

js最基础知识回顾3(字符串拼接,数据类型,变量类型,变量作用域和闭包,运算符,流程控制,)

一.javaScript组成     1.ECMAScript:解释器.翻译 ---------------------------------------------------------几乎没有兼容性问题     2.DOM:Document Object Model --------操作HTML的能力----document--------有一些兼容性问题     3.BOM:Browser Object Model -------------浏览器---------------wind

Java学习笔记之:Java流程控制

一.介绍 Java流程控制包括顺序控制.条件控制和循环控制. 顺序控制,就是从头到尾依次执行每条语句操作.条件控制,基于条件选择执行语句,比方说,如果条件成立,则执行操作A,或者如果条件成立,则执行操作A,反之则执行操作B.循环控制,又称为回路控制,根据循环初始条件和终结要求,执行循环体内的操作. 顺序结构只能顺序执行,不能进行判断和选择,因此需要分支结构. Java有两种分支结构: if语句 switch语句 Java中有三种主要的循环结构: while循环 do…while循环 for循环

操作符&amp;流程控制(PHP笔记)

运算符:算术运算符 赋值运算符  字符串运算符 递增(++)和递减(--)运算符 逻辑运算符 比较运算符 三元运算符       1.算术运算符         算术运算符,用于完成各种算术运算;           + 加法运算      $a + $b           - 减法运算      $a - $b           * 乘法运算      $a * $b           / 除法运算      $a / $b           % 取模运算(求余数)   $a % $b