栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中for和if的使用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中for和if的使用

一、了解什么是while循环以及使用while循环

1. while和if的用法基本类似, 区别在于:if 条件成立,则执行一次; while 条件成立,则重复执行,直到条件不成立为止。

2. 一般情况下,需要多次重复执行的代码,都可以用循环的方式来完成

3. 循环不是必须要使用的,但是为了提高代码的重复使用率,所以有经验的开发者都会采用循环

 

使用while循环输出一个九九乘法表;

for 9x9 

 计算0--100之间所有基数的和 

 

使用for循环输出数字0--9,当数字为6的时候,跳出本次循环,执行其他循环,当数字为8的时候,停止循环

 

使用for循环

# 案例01
# age = 29
# print("---判断开始---")
# if age > 18:
#     print("我已经成年了")
# print("---判断结束---")

# 案例02
# age = 16
# print("---判断开始---")
# if age > 18:
#     print("我已经成年了")
# print("---判断结束---")

# age = int(input("请输入年龄:"))
# if age > 18:
#     print("哥,已成年,终于可以执剑走天涯")

# a = 10
# b = 20
# if a == b:
#     print('True')
# else:
#     print("False")

# score = int(input('请输入成绩:'))
# if score>=90 and score <= 100:
#     print('优秀')

# kicket = 1 #1代表有车票 0代表没有车票
# if kicket == 1:
#     print("有车票,可以上火车")
#     print("可以见到她了,美滋滋~~~")
# else:
#     print("没有车票,不能上火车")
#     print("balabalabalabalabalabala")

# kicket = 1 #1代表有车票 0代表没有车票
# if kicket == 0:
#     print("有车票,可以上火车")
#     print("可以见到她了,美滋滋~~~")
# else:
#     print("没有车票,不能上火车")
#     print("balabalabalabalabalabala")

# sg = int(input("请输入身高:"))
# if sg > 150:
#     print("你的身高已超出免票范围,请买票进入")
# else:
#     print("老虎动物园欢迎你")

# score = int(input("请输入成绩:"))
# if score >= 90 and score <= 100:
#     print("优秀")
# elif score >=80 and score <= 90:
#     print("真棒")
# elif score >= 70 and score <= 80:
#     print("继续努力")
# elif score >=60 and score <= 70:
#     print("一般般")
# else:
#     print("不及格")

# score = int(input("请输入成绩:"))
# if score >= 90 and score <= 100:
#     print("优秀")
# elif score >=80 and score <= 90:
#     print("真棒")
# elif score >= 70 and score <= 80:
#     print("继续努力")
# elif score >=60 and score <= 70:
#     print("一般般")
# else:
#     print("不及格")

# chePiao = int(input("请出示车票:"))
# if chePiao == 1:
#     print("有车票,可以进站")
#     daoLenght = int(input("请输入刀子的长度:"))
#     if daoLenght < 10:
#         print("通过安检")
#         print("终于可以见到他了,美滋滋")
#     else:
#         print("没有通过安检")
#         print("刀子的长度超过鬼将,等待警察处理")
# else:
#     print("没有车票,不能进站")
#     print("亲爱的,那就下次见了")

# chePiao = int(input("请出示车票:"))
# if chePiao == 1:
#     print("有车票,可以进站")
#     daoLenght = int(input("请输入刀子的长度:"))
#     if daoLenght < 10:
#         print("通过安检")
#         print("终于可以见到他了,美滋滋")
#     else:
#         print("没有通过安检")

# gjc = int(input("是否可上上公交车(1.超过两元,0.没有超过两元):"))
# if gjc == 1:
#     print("可以上车")
#     zx = int(input("是否可以坐下(1.可以坐下,0.不可以坐下):"))
#     if zx == 1:
#         print("card_money")
#     else:
#         print("seat")
# else:
#     print("不可以上车")

# player01 = int(input("玩家一请输入(剪刀(0)  石头(1)  布(2):"))
# player02 = int(input("玩家二请输入(剪刀(0)  石头(1)  布(2):"))
# if player01 == 0 and player02 == 2 or player01 == 1 and player02 == 0 or player01 == 2 and player02 == 1:
#     print("玩家一获胜")
# elif player01 == 0 and player02 == 0 or player01 == 1 and player02 == 1 or player01 == 2 and player02 == 2:
#     print("平局")
# else:
#     print("玩家二获胜")

# i = 0
# while i < 10:
#     print("媳妇我错了")
#     i += 1

# i = 0
# # while i < 5:
# #     print("i = %d" % i)
# #     i += 1

# i = 0
# while i < 10:
#     print("我爱你中国,为偶爱你母亲!!!")
#     i += 1

# i = 1
# sum = 0
# while i <= 100:
#     sum += i
#     i += 1
#     print("计算1--100之间的整数和为:%d" % sum)

# i =1
# sum = 0
# while i <= 100:
#     if i % 2 == 0:
#         sum += i
#     i += 1
# print("计算1--100之间的偶数和为:%d" % sum)

# i = 1
# sum = 0
# while i <= 100:
#     if i % 2 == 1:
#         sum += i
#     i += 1
# print("计算1--100之间的奇数和为:%d" % sum)

# i = 1
# while i <= 5:
#     j = 1
#     while j <= i:
#         print("*",end=" ")
#         j += 1
#     print("n")
#     i += 1

# i = 1
# while i <= 9:
#     j = 1
#     while j <= i:
#         print("%d*%d=%d" % (i,j,i*j),end=" ")
#         j += 1
#     print("n")
#     i += 1

# name = "Zhang"
# for i in name:
#     print(i)

for i in range(0,10):
    if i == 6:
        continue
    elif i == 8:
        break
    print(i)
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号