Python通用循环的构造方法实例分析
作者:oOo右右 时间:2022-03-30 00:03:56
本文实例讲述了Python通用循环的构造方法。分享给大家供大家参考,具体如下:
1.交互循环
是无限循环的一种,允许用户通过交互的方式程序的特定部分;
def main():
sum =0.0
count =0
moredata ='yes' #字符串
while moredata[0] =='y': #获取字符串第一个字符,true执行下面的
x =eval(input('enter a number>>'))
sum =sum+x
count = count +1
moredata =input("你有更多的number(yes or no)")
print("pingjunshushi:\n",sum/count)
main()
enter a number >>3
你有更多的number(yesor no)y
enter a number >>4
你有更多的number(yesor no)n
pingjunshushi:
3.5
2.哨兵循环:
执行循环要遇到设定的特殊数据,循环语句才会终止。
哨兵循环求平均数的方法:
1)设定一个哨兵值作为循环终止的标识;
2)任何值都可以看做哨兵,但是要与实际数有所区别;
python中空字符串以双引号""表示,注意引号中间没有空格!!!
def main():
sum =0
count =0
xStr = input("enter a number")
while xStr != "": #空字符串
x = eval(xStr) #转换字符串为数字的过成
sum = sum + x
count =count +1
xStr = input("enter a number :")
print("average is",sum/count)
main()
enter a number6
enter a number :6
enter a number :6
enter a number : 这里确认输入不是哨兵空字符才将输入字符串转换为数字
average is 6.0
eval()
函数参数是字符串可以当成有效python表达式来求值,并返回计算结果
3.文件循环
def main():
fileName = input("file denumber:") #这个相当于一个文件
infile = open(fileName,'r') #open(文件名,方式'r'/'w')函数用来打开这个文件的一行保存在infile列表(相当一个数组)中
sum =0
count = 0
for line ininfile: #循环变量line遍历文件的每一行(文件每一行已经保存在infile列表中),将每一行执行下面的代码
sum = sum+eval(line)
count = count +1
print("aveage is:",sum/count)
main()
循环遍历文件,通常的方法是用哨兵方法一次读取文件的一行
这个可以用来读取excel中的测试用例;
python中采用readline()
方法的end-of-file循环模式:
readline()
将文件的一行读取到字符串中,在文件尾部readline()
返回一个空字符串可以作为哨兵值;
line=infile.readline()
while line != "":
#处理每一行
line =infile.readline()
这段代码会让人误以为遇到一个空行时就会退出,其实不然文本文件的空行包括一个换行符\n 这样readline()函数返回值是换行符,而不是哨兵值空字符串,循环继续
open()
打开文件读取保存到list中
readline()
读取文件,每次读取一行
4.死循环
python中可以用python完成特定的功能:
while True:
try:
x = int(input("输入一个数字:"))
break
except ValueError:
print("重新输入:")
copy()
函数:
返回字典的浅拷贝;
dict1={"name":"liyue"}
dict2=dict1.copy()
print("dict2 is:",str(dict2))
运行结果:
dict2 is: {'name': 'liyue'}
python读取excel:
注意读取时候加上表头,没表头不行,所以测试用例要有表头。
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/JOJOY_tester/article/details/53231603
标签:Python,循环,构造方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL UPDATE delete 语句的速度
2008-03-12 12:22:00
OpenCV 图像梯度的实现方法
2023-07-14 08:25:43
![](https://img.aspxhome.com/file/2023/6/64296_0s.png)
jQuery.sheet – 创建Excel界面风格的jQuery在线应用
2010-01-27 13:03:00
![](https://img.aspxhome.com/file/UploadPic/20101/27/20101271369489s.jpg)
解决BN和Dropout共同使用时会出现的问题
2023-05-08 14:08:19
![](https://img.aspxhome.com/file/2023/9/83949_0s.png)
本地计算机无法启动Apache故障处理
2023-07-17 13:06:48
Python多线程的使用详情
2023-05-29 15:13:36
Go微服务网关的实现
2023-07-02 22:27:05
![](https://img.aspxhome.com/file/2023/7/87627_0s.png)
解决python3.6用cx_Oracle库连接Oracle的问题
2023-06-12 02:38:46
![](https://img.aspxhome.com/file/2023/7/59827_0s.png)
iframe 的用法与注意事项
2008-02-12 12:56:00
ASP同一站点下gb2312和utf-8页面传递参数乱码的终极解决方法
2011-02-20 11:00:00
asp程序运行速度测试
2008-02-11 19:11:00
python3下载抖音视频的完整代码
2023-12-24 01:10:42
DIV+CSS高度自适应网页代码实例
2008-09-20 08:00:00
![](https://img.aspxhome.com/file/UploadPic/20089/20/20089208546752s.jpg)
asp GetString的用法
2008-06-12 13:46:00
关于select元素的两个小问题
2008-12-25 13:41:00
一个不错网速测试代码
2008-07-20 13:41:00
Python企业编码生成系统之主程序模块设计详解
2023-11-18 18:44:37
python使用redis模块来跟redis实现交互
2023-05-06 04:13:40
![](https://img.aspxhome.com/file/2023/6/78476_0s.png)
Python面向对象编程关键深度探索类与对象
2021-12-07 03:06:20
神经网络python源码分享
2021-10-07 10:41:00
![](https://img.aspxhome.com/file/2023/0/90790_0s.jpg)