首页 > 编程语言 >笨办法学Python3 习题33 while 循环

笨办法学Python3 习题33 while 循环

时间:2023-10-12 20:45:12浏览次数:46  
标签:笨办法 bottom 33 top shuzu while 循环 习题 now

while 循环

  • 只要循环语句中的条件布尔值为True ,就会不停的执行下面的代码块 命令。
  • while循环是无边界循环,for in 循环是有边界循环
  • 和 if 语句的相似点都是检查一个布尔表达式的真假,if 语句是执行一次,while 循环是执行完跳回到while 顶部,如此重复,直到布尔值为假False
  • 尽量少用 while 循环,大部分时候用 for in 循环是更好选择

 

 1 i = 0                          # 第一种while 循环 写法
 2 numbers = []                   # 把空数组赋值给变量numbers
 3 
 4 while i < 6:                   # 进入while 循环 当i<6:
 5     print(f'At the top is {i}')    # 打印  i的头部数
 6     numbers.append(i)              # 变量numbers 尾部追加 i 数字
 7 
 8     i+=1                           # i 每次自增1
 9     print("Numbers now:",numbers)  # 记得加逗号//打印 数组现在:
10     print(f"At the bottom is {i}") # 打印 i的底部数
11 
12 print("The numbers :")         # 打印 这个数字是: 
13 for num in numbers:            # 用for循环来循环数组numbers
14     print(num)                      # 打印 num 变量
15 
16 
17 
18 
19 shuzu = []                      # 第二种for in 循环语句改写
20 
21 for t in range(0,6):
22     print(f"top of t:{t}")
23     shuzu.append(t)
24     print(f"shuzu:",shuzu)
25     t+=1
26     print(f"bottom of t:{t}")
27 
28 print(f"The shuzu:")
29 for shu in shuzu:
30     print(shu)

 

PS C:\Users\Administrator\lpthw> python ex33.py
At the top is 0
Numbers now: [0]
At the bottom is 1
At the top is 1
Numbers now: [0, 1]
At the bottom is 2
At the top is 2
Numbers now: [0, 1, 2]
At the bottom is 3
At the top is 3
Numbers now: [0, 1, 2, 3]
At the bottom is 4
At the top is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom is 5
At the top is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom is 6
The numbers :
0
1
2
3
4
5
top of t:0
shuzu: [0]
bottom of t:1
top of t:1
shuzu: [0, 1]
bottom of t:2
top of t:2
shuzu: [0, 1, 2]
bottom of t:3
top of t:3
shuzu: [0, 1, 2, 3]
bottom of t:4
top of t:4
shuzu: [0, 1, 2, 3, 4]
bottom of t:5
top of t:5
shuzu: [0, 1, 2, 3, 4, 5]
bottom of t:6
The shuzu:
0
1
2
3
4
5

 

 

 

标签:笨办法,bottom,33,top,shuzu,while,循环,习题,now
From: https://www.cnblogs.com/luxiaoli/p/17760495.html

相关文章

  • Tire树练习题
    Tire树练习题T1「一本通2.3例2」TheXORLargestPair在给定的\(N\)个整数\(A_1,A_2,...A_N\)中选出两个进行异或运算,得到的结果最大是多少?数据范围对于100%的数据,\(1\leqN\leq10^5,0\leqA_i\leq2^{31}\)1.朴素算法并用二分查去匹配二进制下每个数最高位的0还......
  • 动态规划习题
    DP习题Melon的难题【01背包问题中“装满背包的最少物品数问题】注意初始化问题,第一行除了第一个都要赋值最大值!!!importjava.util.Scanner;importjava.util.*;//注意类名必须为Main,不要有任何packagexxx信息publicclassMain{publicstaticvoidmain(String......
  • CF1333A [Little Artem]
    Problem题目简述给你一个\(n\timesm\)的方格,构造一个方案,使得方案中\(B=W+1\)。\(B\):相邻的格子有至少一个白色格子的黑色格子的个数。\(W\):相邻的格子有至少一个黑色格子的白色格子的个数。思路分奇偶讨论。\(n\timesm\)是偶数:构造一张黑、白相间的矩阵,左上......
  • 笨办法学Python3 习题32 循环和列表
    知识点:for i in y: #for循环开始i变量就被创建,所以不用提前创建只有在for循环里有效range(,)函数会从第一个数到最后一个之前的数,不包含最后一个数Y.append(X)将X追加到列表Y的尾部1the_count=[1,2,3,4,5]#创建3个列表变量2fr......
  • Windows更换默认远程端口3389
    直接上方法:1、打开注册表2、打开路径“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer\Wds\rdpwd\Tds\tcp”,修改“PortNumber”4、打开路径“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp”,修改“Port......
  • 洛谷P3300 [SDOI2013] 城市规划 题解
    [SDOI2013]城市规划题意:给你一个\(6\timesn\)的网格题,单点修改,询问区间联通块数,\(n\le10^5\)。解:看起来就很显然的一道题......线段树每个点用一个ufs维护连通性;我为了方便思考把图转成横着的了。写起来真是毒瘤......重点在于:\(\bullet\)1、建立叶节点。\(\bull......
  • 习题专题
    习题十:打印素数#include<stdio.h>intmain(){ inti=0; intj=0; for(i=100;i<=200;i++) { for(j=2;j<i;j++)//素数除了1和他本身不能再被整除的数字 { if(i%j==0) { break; } } if(i==j) { printf("%d",i);......
  • python练习题(一)
    算法题1.计算1-100之间所有偶数的和#定义一个变量用来保存最后的累加和total_even_sum=0#从1到100的数fornuminrange(1,101):#判断是否为偶数ifnum%2==0:total_even_sum+=numprint("偶数和是:",total_even_sum)2.计算1-100的和#......
  • python练习题(二)
    文件操作1.读取一个文本文件,打印文件内容到控制台。defprint_file_content(file_path):try:withopen(file_path,'r')asfile:content=file.read()print(content)exceptExceptionase:print(f"没有找到文件:{e}"......
  • 笨办法学Python3 习题30 else 和 if
    1people=302cars=403trucks=1545ifcars>people:#下面同时为True,也只会运行第一个为True的块,另外两个优先级依次低于if6print("Weshouldtakethecars.")#第一个分支的块78elifcars<people:......