Python基础语法——循环(一)
2017-02-19 17:18
435 查看
学习资料:
《Python语言程序设计》——梁勇(著)
循环设计策略:
确认需要循环的语句
将循环语句整合在一个循环中
编写循环继续条件并添加合适语句进行控制循环,避免无限循环
注意:执行循环经常会遇到偏离1的误差,比如想要循环100次但是循环条件会写成:
for循环的语法:
for循环会进行遍历sequence中的所有条目,可以将条目数量作为循环的次数。
嵌套循环在什么情况下需要使用呢?
回想一下乘法表有什么特性呢?没错就是1-9每位数都要和这9个数进行乘法,像这种每个值都需要进行同等长度数值的遍历处理的情况就需要嵌套循环了。
注意:嵌套循环不能太多层,否则程序的效率会降低。
9ff0
编程,下一篇会对循环的易错点和实例进行深入实现和分析。
《Python语言程序设计》——梁勇(著)
前言
今天继续学习了Python基本语法中的循环部分,用于执行大量需要重复的命令,主要是while和for循环,同时学会使用break和continue进行控制循环。while循环
这是一种条件控制型的循环,只有满足判断才能执行循环体语句,否则直接执行循环体之后的语句。while loop-continuation-condition: statement(s) #eg: i = 1 while i < 10: print(i) i += 1
循环设计策略:
确认需要循环的语句
将循环语句整合在一个循环中
编写循环继续条件并添加合适语句进行控制循环,避免无限循环
注意:执行循环经常会遇到偏离1的误差,比如想要循环100次但是循环条件会写成:
count = 0;count<=100这样实际执行了101次因而在进行有长度限制的操作时会引发报错。
编写任务:四则运算测验工具
通过循环和交互控制实现四则运算的测验,测验结束汇报结果及时间。#!/usr/local/bin/python # encoding: utf-8 ''' test -- 软件流程开发学习 -- 循环,四则测验,改编5-4 @author: Eric @copyright: 2017 organization_name. All rights reserved. @contact: xj_lin@protonmail.com @param version:v1.0 ''' import time import random import sys def Four_Operations_Quiz_Loop(continueloop): while continueloop == "Y": correctCount = 0 count = 0 NUMBER_of_QUESTIONS = eval(input("现在开始100以内四则运算测验,请输入测验题数:")) startTime = time.time()#get start time while count < NUMBER_of_QUESTIONS: #运算选择 choice = random.randint(0,3) if choice != 3: num1 = random.randint(0,99) num2 = random.randint(0,99) else: num1 = random.randint(1,99) num2 = random.randint(1,99) if choice == 0: #加法 answer = eval(input("请计算:"+str(num1)+"+"+str(num2)+"=")) if answer == (num1 + num2): print("Correct!") correctCount += 1 else: print("Error!\nThe correct answer is "+str((num1 + num2))+".") elif choice == 1: #减法 #if num1 > num2,swap num2 with num1 if num1 < num2: num1,mum2 = num2,num1 answer = eval(input("请计算:"+str(num1)+"-"+str(num2)+"=")) if answer == (num1 - num2): print("Correct!") correctCount += 1 else: print("Error!\nThe correct answer is "+str((num1 - num2))+".") elif choice == 2: #乘法 answer = eval(input("请计算:"+str(num1)+"×"+str(num2)+"=")) if answer == (num1 * num2): print("Correct!") correctCount += 1 else: print("Error!\nThe correct answer is "+str((num1 * num2))+".") else: #整数除法 if num1 < num2: num1,num2 = num2,num1 #if num2 == 0 or Not divisible while (num1 / num2)%1 != 0 and (num2 / num1)%1 != 0: #不能整除,重选除数和被除数 num1 = random.randint(1,99) num2 = random.randint(1,99) if num1 < num2: num1,num2 = num2,num1 answer = eval(input("请计算:"+str(num1)+"÷"+str(num2)+"=")) if answer == int(num1 / num2): print("Crrect!") correctCount += 1 else: print("Error!\nThe correct answer is "+str(int(num1 / num2))+".") count += 1 endTime = time.time()#get end time testTime = endTime - startTime#get test time print("Correct count is "+str(correctCount)+" out of "+str(NUMBER_of_QUESTIONS)+" ,correct rate is "+str(format(correctCount / NUMBER_of_QUESTIONS,".2%"))+" !\nTest time is "+str(format(testTime,".3f"))+"s.") continueloop = input("是否进行新测验:是的话输入Y继续测验,否则输入N退出测验!\n") if continueloop == "N": print("测验结束。") sys.exit() if __name__ == "__main__": continueloop = "Y" Four_Operations_Quiz_Loop(continueloop)
for循环
Python中的for循环和其他语言有些不同,Python中的for循环是通过一个序列中的每个值来进行迭代。for循环的语法:
for var in sequence: #loop body #eg: for i in range: print(i)
for循环会进行遍历sequence中的所有条目,可以将条目数量作为循环的次数。
嵌套循环
顾名思义便是在一个循环体中存在另一个循环体,有多重循环,可以使用for的嵌套循环实现乘法表;嵌套循环在什么情况下需要使用呢?
回想一下乘法表有什么特性呢?没错就是1-9每位数都要和这9个数进行乘法,像这种每个值都需要进行同等长度数值的遍历处理的情况就需要嵌套循环了。
注意:嵌套循环不能太多层,否则程序的效率会降低。
小结
今天主要是对循环的基本语法进行熟悉并进行实践9ff0
编程,下一篇会对循环的易错点和实例进行深入实现和分析。
相关文章推荐
- Python基础语法——循环(五)
- Python语法基础(八):while循环
- Poedu_Python_Lesson003_基础语法_列表、元组、判断、循环
- Python语法基础11.for循环
- Python基础语法——循环(三)
- Python基础语法学习--条件控制与循环语句
- Python语法基础10.while循环
- python基础语法——循环(二)
- Python基础语法——循环(四)
- Python 基础语法(四)
- Python 基础语法知识(二)
- Python2 基础语法(四)
- Python基础教程学习笔记----第五章 条件、循环和其他语句
- Python 基础语法知识(三)
- python学习日记1--基础语法篇
- shell编程基础自学知识(截取字符串) shell基础语法判断和循环等
- Python2 基础语法(二)
- Python2 基础语法(一)
- javascript学习系列02(变量,循环,基础语法)
- Python基础06 循环