2017-12-02 12:36:35 December Saturday the 48 week, the 336 day
#原创第13篇~while循环
#普通while
num = 1 #num is number
while num <= 3: #while执行后面语句块
print(num) #每次循环让num加1
num += 1 #等价于num = num +1
2017-12-02 12:36:39 December Saturday the 48 week, the 336 day
#原创第13篇~while循环
#while和input函数
robote = "\nplease talk to me, and I will repeat what you say:"
robote += "\nEnter 'Q' to end the program.\n" #用户输入Q程序结束
words = ""
while words != 'Q':
words = input(robote)
print(words)
2017-12-02 13:04:09 December Saturday the 48 week, the 336 day
#原创第13篇~while循环
#while和else语句
num = 1 #num is number
while num <= 3: #while执行后面语句块
print(num) #每次循环让num加1
num += 1 #等价于num = num +1
else:
print("num is not less than 3") #num 不再小于数字3了
#只有当while为假,全部执行完成后才执行else后面的语句。
2017-12-02 13:44:32 December Saturday the 48 week, the 336 day
#原创第13篇~while循环
#while和break语句
k = 1
while 1: # 循环条件为1必定成立
if k > 5: # 当i大于5时跳出循环
break #k为6时候跳出while循环,不再输出k的值
print(k) # 输出1~5
k += 1 #每循环一次,让k的值加1,等价于:k = k + 1
2017-12-03 17:59:59 December Sunday the 48 week, the 337 day
#原创第13篇~while循环
#while and continue
'''
i是奇数时,if条件成立,接着执行continue,接着跳过print(i)函数,
返回去重新执行while条件语句i < 10
i是偶数时候,if语句不成立,跳过continue语句,执行print(i),
然后返回去重新执行while条件语句i < 10
'''
i = 1
while i < 10:
i += 1
if i%2 > 0:
continue #
print(i) # 输出双数2、4、6、8、10
2017-12-03 18:44:49 December Sunday the 48 week, the 337 day
#原创第13篇~while循环
#while和true and false语句
robote = "\nplease talk to me, and I will repeat what you say:"
robote += "\nEnter 'quit' to end the program.\n" #用户输入Q程序结束
condition = True
words = ""
while condition:
words = input(robote) #words得到输入的字符
if words == "quit":
condition = True #用户一旦输入quit,while循环就结束
else:
print(words) #只要用户没有输入quit,就一直输出输入的内容
2017-12-03 19:02:13 December Sunday the 48 week, the 337 day
#原创第13篇~while循环
#控制台运行python程序,ctrl + C 结束 while死循环
while 1: #条件始终成立,while将会被一直执行
print("I love Python") #唯有Ctrl + C 让我不会再爱python
2017-12-03 20:02:13 December Sunday the 48 week, the 337 day
#原创第13篇~while循环
#课后题:99乘法口诀
i=1 #i是行数
while i<=9: #外边一层循环控制行数
j=1 #j是列数
while j<=i: #里面一层循环控制每一行中的列数
nine_nine =j*i
print("%d*%d=%d"%(j,i,nine_nine), end=" ")
#print(" ")
j+=1
print(i,"行结束")
#print("")
i+=1
1*1=1 1 行结束
1*2=2 2*2=4 2 行结束
1*3=3 2*3=6 3*3=9 3 行结束
1*4=4 2*4=8 3*4=12 4*4=16 4 行结束
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 5 行结束
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 6 行结束
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 7 行结束
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 8 行结束
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 9 行结束
[Finished in 0.4s]
python#原创第13篇~while循环+答案
最新推荐文章于 2023-12-10 13:30:09 发布