python中continue格式_python自学(3)--for 、 while、 break、continue、字符格式化输出...
一、循環語句
1.for 語句
有限循環 ,次數限制
格式: for i in range (3):
例子:
_username = "zhichengfang"
_password = "fzc2551352"
for i in range(3) :
username = input("username :")
psworrd = input("password :")
if username == _username and psworrd == _password :
print("welcome login of %s"%_username)
break
else :
print("the username or password is wrong you will hve %s times"% str(2-i))
2. while 語句
無限循環=死循環
格式: while 條件 :
例子:
# _author : Administrator
# date : 2020/4/26 0026
_username = "zhichengfang"
_password = "fzc2551352"
counter = 1
while counter < 4:
username = input("username :")
psworrd = input("password :")
if username == _username and psworrd == _password :
print("welcome login of %s"%_username)
break
else :
print("the username or password is wrong ")
counter += 1
if counter == 4 :
try_again = input("try again? [y/n]")
if try_again == "y":
counter = 1
else:
print("get out my area, fuck you bitch")
3.break 、continue
continue 結束本次循環,繼續下一次循環 ,continue語句被用來告訴Python跳過當前循環塊中的剩余語句,然后 繼續 進行下一輪循環
例子:
#!/usr/bin/python
# Filename: continue.py
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
break 跳出整個當前的循環
例子:
for i in range (3):
print(i)
for j in range (3):
print(j)
break #跳出當前循環,跳出第二層循環,第一層不受影響
4,常用占用符:
常見的占位符有:
%d 整數
%f 浮點數
%s 字符串
%x 十六進制整數
使用方法:
tpl= "i am %s" % "alex"
tpl= "i am %s age %d" % ("alex",18)
tpl= "i am %(name)s age %(age)d" % {"name":"alex","age":18}
tpl= "percent %.2f" % 99.97623
tpl= "i am %(pp).2f" % {"pp":123.425556, }
tpl= "i am %.2f %%" % {"pp":123.425556, }
例子:
name = input("name is :") #用戶交互 input()
age= input("age is :")
job= input("job is :")
salary = input("salary is :")
msg = '''
---------info of %s---------
Name: %s
Age : %s
Job : %s
salary : %s
----------------------------
'''%(name,name,age,job,salary) #msg = 被賦值為字符串
print(msg)
總結
以上是生活随笔為你收集整理的python中continue格式_python自学(3)--for 、 while、 break、continue、字符格式化输出...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 八、pink老师的学习笔记—— CSS用
- 下一篇: Vue移动端项目——搜索联想建议功能的实