目录
本期,我们会学习 Python 中的 循环语句
PS: 我在 第四期 中提到了 循环语句,但是其实还有一个 while 循环语句。
简介
在编写代码时,我们很难避免要重复运用同一个代码。但是你总不可能总是复制粘贴啊,这样不仅麻烦,还会增加文件大小。
用法
普通
a=0 b=100 while a<b: a=a+1 print(a)
无限循环
a=233 while a==233: var=input("1+1=") print(var, "is the wrong answer. Please Enter the correct answer.\n")
嵌套 if, else, elif
n=233 i=input("Guess a number from 1-1000\n>") i=int(i) while i==233: print("Wow, you're right! Congratulations!") i=0 # 避免出现无限循环 else: print("")