首页 > 分享 > Python 下input,while True ,continue,break,for等的用法

Python 下input,while True ,continue,break,for等的用法

最新推荐文章于 2023-04-15 16:00:11 发布

hellenlee22 于 2020-02-21 18:53:48 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

input用法

用户输入: >>> name = input("请输入你的姓名") 请输入你的姓名lj >>> name 'lj' >>> 1234567

while True 的用法

my_age = 18 count=5 while True: age = int(input("请输入年龄")) if age == my_age: print("you are right!") break elif age > my_age: count -= 1 print("bigger,还有{}次输入机会".format(count)) elif age < my_age: count-=1 print("smaller,还有{}次输入机会".format(count)) continue

12345678910111213141516

结果如下:C:Python36python3.exe C:/Users/Administrator/Desktop/01.py 请输入年龄1 smaller,还有4次输入机会 请输入年龄19 bigger,还有3次输入机会 请输入年龄20 bigger,还有2次输入机会 请输入年龄7 smaller,还有1次输入机会 请输入年龄18 you are right! Process finished with exit code 0 12345678910111213

位运算符用法:

在这里插入图片描述

for循环

for i in range(0,10): if i <5: continue print(i) C:Python36python3.exe C:/Users/Administrator/Desktop/01.py 5 6 7 8 9 1234567891011

break 和continue的区别 1

for i in range(0,10): if i >5: #break continue print(i) C:Python36python3.exe C:/Users/Administrator/Desktop/01.py 0 1 2 3 4 5 Process finished with exit code 0 for i in range(0,10): if i >5: break #continue print(i) C:Python36python3.exe C:/Users/Administrator/Desktop/01.py 0 1 2 3 4 5 Process finished with exit code 0 ************************************************** for i in range(0,10): if i <5: #break continue print(i) C:Python36python3.exe C:/Users/Administrator/Desktop/01.py 5 6 7 8 9 Process finished with exit code 0 for i in range(0,10): if i <5: break #continue print(i) C:Python36python3.exe C:/Users/Administrator/Desktop/01.py Process finished with exit code 0

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768

字符串格式化输出

name = "Duke" print("i am %s"%name) 12

常用-----> 字符串 %s;整数 %d;浮点数%f

另一种实现方法之format()方法

print('Hi,{name},{message}'.format(name = 'Duke',message = 'i am in Chengdu')) 1

参考:https://www.cnblogs.com/huigebj/p/11298332.html
https://www.cnblogs.com/dukeidea/p/6362031.html

相关知识

Python 下input,while True ,continue,break,for等的用法
开始Python学习第五课(python控制,循环语句)
python编程
python 练习题
Python宠物养成系统3.0
Python宠物养成系统1.0
六、Python 基础语句
python写的贪吃蛇改进版
Python学习第一期
python语法认为条件x

网址: Python 下input,while True ,continue,break,for等的用法 https://m.mcbbbk.com/newsview808612.html

所属分类:萌宠日常
上一篇: DNF战令4:阿拉德高校时装、武
下一篇: 记一次selenium + Ch