一、if判断

1、语法一:

if 条件:
    条件成立时执行的子代码块
    代码1
    代码2
    代码3

示例:

sex='female'
age=
is_beautiful=True

 and age <  and is_beautiful:
    print('开始表白。。。')

print('other code1...')
print('other code2...')
print('other code3...')

2、语法二:

if 条件:
    条件成立时执行的子代码块
    代码1
    代码2
    代码3
else:
    条件不成立时执行的子代码块
    代码1
    代码2
    代码3

示例:

sex='female'
age=
is_beautiful=True

 and age <  and is_beautiful:
    print('开始表白。。。')

else:
    print('阿姨好。。。')                      阿姨好。。。

print('other code1...')
print('other code2...')
print('other code3...')

3、语法三:

if 条件1:
    if 条件2:
        代码1
        代码2
        代码3

示例:
sex='female'
age=
is_beautiful=True
is_successful=True
height=1.70

 and age <  and is_beautiful \
        and height > 1.60 and height < 1.80:
    print('开始表白。。。')
    if is_successful:
        print('在一起。。。')
    else:
        print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')
else:
    print('阿姨好。。。')

print('other code1...')
print('other code2...')
print('other code3...')

4、语法四:

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

示例:
如果成绩 >= ,那么:优秀

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

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

其他情况:很差

score = input('
score = int(score)

:
    print('优秀')
elif score >= :
    print('良好')
elif score >= :
    print('普通')
else:
    print('很差')

二、while循环。while可以嵌套使用,以下案例有解释说明。

语法:

while 条件:
    代码1
    代码2
    代码3

例如:
while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    ':
        print('login successful')
    else:
        print('username or password error')
# while True:
#     name = input('我的暗号是:>>>')
#     password = input('请输入下一句:>>>')
#     if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
#         print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
#     else:
#         print('输入错误,你是间谍,就地拿下')

# 打印出1-10个数
# count =
# :
#     print(count)
#     count += 

1、结束while循环的两种方式:

方式一:条件改为false:条件mag改为False。条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。

# 方式一:条件mag改为False。
#         条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。
# mag = True
# while mag:
#     name = input('我的暗号是:>>>')
#     password = input('请输入下一句:>>>')
#     if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
#         print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
#         while mag:
#             print("""
#              会话结束
#              你现在正在进行的工作
#              你的名字
#              你今天洗澡了吗
#             """)
#             choice = input('你想知道的我都可以告诉你,你问吧')
#             ':
#                 mag = False
#             elif choice == ':
#                 print('你现在正在进行的工作')
#             elif choice == ':
#                 print('你的名字')
#             elif choice == ':
#                 print('你今天洗澡了吗')
#             else:
#                 print('我不想回答')
#
#     else:
#         print('输入错误,你是间谍,就地拿下')

方式二:while + break:break 一定是在循环体内,当程序运行到break时,就会立刻结束本层循环。

# while True:
#     name = input('我的暗号是:>>>')
#     password = input('请输入下一句:>>>')
#     if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
#         print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
#         while True:
#             print("""
#              会话结束
#              你现在正在进行的工作
#              你的名字
#              你今天洗澡了吗
#             """)
#             choice = input('你想知道的我都可以告诉你,你问吧')
#             ':
#                 break
#             elif choice == ':
#                 print('你现在正在进行的工作')
#             elif choice == ':
#                 print('你的名字')
#             elif choice == ':
#                 print('你今天洗澡了吗')
#             else:
#                 print('我不想回答')
#         break
#     else:
#         print('输入错误,你是间谍,就地拿下')

while + continue:是在循环体内结束本次循环,直接执行下一次循环。

# 打印出1-10个数
# count =
# :
#     print(count)
#     count += 

# 打印1-,-,不打印6和7
# count =
# :
#      or count == :
#         continue
#     print(count)
#     count += 

三、for循环:for 循环能解决的,while循环都能解决。

但是为什么还要使用for循环呢,是因为在特定的情况下,使用for循环更简便一些。

它的强大之处在于能循环取值。

# l = ['a', 'b', 'c', 'd', 'e']
# i =
# while i < len(l):
#     print(l[i])
#     i +=          # 结果为
# b
# c
# d
# e

# name = ['jerry', 'tommy', 'anne', 'una']
# for x in name:
#     print(x)

1、for + break

# name = ['jerry', 'tommy', 'anne', 'una']
# for x in name:
#     if x == 'anne':
#         break
#     print(x)

2、for + continue

# name = ['jerry', 'tommy', 'anne', 'una']
# for x in name:
#     if x == 'anne':
#         continue
#     print(x)

3、for + else

# name = ['jerry', 'tommy', 'anne', 'una']
# for x in name:
#     if x == 'zoe':
#         break
#     print(x)
#
# else:
#     print('其他')

4、for + range

# for + range   #  顾头不顾尾
# , ):
#     print(x)                 # 结果为
#
#
#
#
# 

5、for 嵌套:顾头不顾尾。range(1,5) 取值为 1 2 3 4

# , ):
#     , ):
#         print(x, y)    # 结果为
#
#
#
#
#
#
#
#  

四、课后作业

1、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

name = input('请输入用户名:>>>')
password = input('请输入密码:>>>')
':
    print('登录成功')

else:
    print('登录失败')

2、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

count =
:
    name = input('请输入用户名:>>>')
    password = input('请输入密码:>>>')
    ':
        print('登录成功')
        break
    else:
        print('登录失败')
        count += 

3、求1-2+3-4+5 ... 99的所有数的和

res =
count =
:
     == :
        res -= count
    else:
        res += count
    count +=
print(res)

4、使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12

count =
:

     or count == :
        count +=
        continue
    print(count)
    count += 

5、使用 while 循环实现输出 1-100 内的所有奇数

count =
:
     == :
        print(count)
    count += 

使用 while 循环实现输出 1-100 内的所有奇数

count =
:
     == :
        print(count)
    count += 

6、用户登陆(三次机会重试)

count=
:
    name=input('请输入用户名:')
    password=input('请输入密码:')
    ':
        print('login success')
        break
    else:
        print('用户名或者密码错误')
        count+=

7、猜年龄游戏

age_of_oldboy=

count=
:
    guess=int(input('>>: '))
    if guess == age_of_oldboy:
        print('you got it')
        break
    count+=

8、猜年龄游戏升级版

要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出

age_of_oldboy=

count=
while True:
    :
        choice=input('继续(Y/N?)>>: ')
        if choice == 'Y' or choice == 'y':
            count=
        else:
            break

    guess=int(input('>>: '))
    if guess == age_of_oldboy:
        print('you got it')
        break
    count+=

day4 四、流程控制之if判断、while循环、for循环的更多相关文章

  1. 基础运算符补充,流程控制之if判断/while循环

    常量 常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量. AGE_OF_OLDBOY = 56 基础运算符补充 1.算术运算 加减乘除+ - ...

  2. 格式化输出的三种方式,运算符及流程控制之if判断

    ''' 格式化输出的三种方式,运算符及流程控制之if判断 ''' # 格式化输出的三种方式 # 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式 比如要求用户输入用户名和年龄 ...

  3. [基本运算符、流程控制之if判断、与用户交互、深浅拷贝]

    [基本运算符.流程控制之if判断.与用户交互] 基本运算符 1.算数运算符 python支持的算术运算符与数学上计算的符号使用是一致的 salary = 3.3 res = salary * 12 p ...

  4. C 碎片四 流程控制

    前面介绍了程序中用到的一些基本要素(常量,变量,运算符,表达式),他们是构成程序的基本成分,下面将介绍C语言中流程控制的三种结构:顺序结构.分支结构.循环结构 一.顺序结构 顺序结构的程序设计是最简单 ...

  5. java 基础知识四 流程控制

    java   基础知识四 流程控制 Java流程控制包括顺序控制.条件控制和循环控制 顺序控制就是逐条执行 有if和switch两个分支 循环控制就是 又称为回路控制,根据循环初始条件和终结要求,执行 ...

  6. python小白——进阶之路——day4天-———流程控制while if循环

    # ### 代码块: 以冒号作为开始,用缩进来划分作用域,这个整体叫做代码块 if 5 == 5: print(1) print(2) # 注意点: 要么全部使用4个空格,要么全部使用1个缩进 ,这样 ...

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

    if判断? 什么是if判断? 判断一个条件如果成立则做...不成立则... 为什么要有判断? 让计算机像人一样具备判断的能力 如何用if判断 if 条件1: code1    code2    cod ...

  8. Java学习day4 程序流程控制一

    一.分支结构 条件语句:if...else if语句: 一个 if 语句包含一个布尔表达式和一条或多条语句,如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代 ...

  9. 流程控制之if判断

    目录 语法(掌握) if if...else if...elif...else 练习(掌握) 练习1:成绩评判 练习2:模拟登录注册 if的嵌套(掌握) 语法(掌握) if判断是干什么的呢?if判断其 ...

随机推荐

  1. JPA中自动使用@Table(name = "userTab")后自动将表名、列名添加了下划线的问题

    一.问题 JPA中自动使用@Table(name = "userTab")后自动将表名.列名添加了下划线的问题,如下图: 二.解决 在application.properties文 ...

  2. spring mvc 链接 postgresql

    上一篇文章已经分享了搭建springmvc:http://www.cnblogs.com/liqiu/p/4252788.html 这一篇来链接数据库postgresql 1.在pom.xml添加几个 ...

  3. Redis集群搭建(转自一菲聪天的“Windows下搭建Redis集群”)

    配置Redis参考:http://blog.csdn.net/zsg88/article/details/73715947 使用Ruby配置集群参考:https://www.cnblogs.com/t ...

  4. 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

  5. Python gensim库word2vec 基本用法

    ip install gensim安装好库后,即可导入使用: 1.训练模型定义 from gensim.models import Word2Vec   model = Word2Vec(senten ...

  6. SourceInsight: sourceInsight4.0 修改默认字体

    快捷键 Alt + Y

  7. 【Linux高级驱动】linux设备驱动模型之平台设备驱动机制

    [1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数  a.申请主设备号    register_chrdev(major,name,file_operations);  b.创 ...

  8. docker内程序如何读取dockerfile和compose.yml中设置的环境变量

    docker内程序如何读取dockerfile和compose.yml中设置的环境变量 背景 compose文件中配置了服务A和服务B,其中B服务调用了A服务的接口,那么B的实现代码中该如何调用A的服 ...

  9. [转]PowerDesigner大小写转换

    原文地址:https://blog.csdn.net/fzqlife/article/details/72769959?utm_source=blogxgwz7 在菜单栏找到:Tools-->E ...

  10. 树莓派配置tomcat

    先安装配置好apache apt-get install apache2 /etc/init.d/apache2 start (blog passage from http://www.cnblogs ...