上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture05 ITERATION Control Structure

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Computational Thinking and Approach Lecture 5 Dr.Jialiang LU Jialiang.lu@situ.edu.cn
Computational Thinking and Approach Lecture 5 Dr. Jialiang LU Jialiang.lu@sjtu.edu.cn 1

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Control Structure ITERATION 2
ITERATION Control Structure 2

@ 上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Out ines 。for:definite Loop while:indefinite loop ·Interactive loop ● Sentinel loop 。File loop 。Nested loop ·Dictionary 3
3 Outlines • for: definite Loop • while: indefinite loop • Interactive loop • Sentinel loop • File loop • Nested loop • Dictionary

上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review The for statement allows us to iterate through a sequence of values. for in : -for i in range(n): The loop index variable var takes on each successive value in the sequence,and the statements in the body of the loop are executed once for each value. 4
4 For Loops: A Quick Review • The for statement allows us to iterate through a sequence of values. • for in : – for i in range(n): • The loop index variable var takes on each successive value in the sequence, and the statements in the body of the loop are executed once for each value

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review ● Suppose we want to write a program that can compute the average of a series of numbers entered by the user. e To make the program general,it should work with any size set of numbers. We don't need to keep track of each number entered,we only need know the running sum and how many numbers have been added. 5
5 For Loops: A Quick Review • Suppose we want to write a program that can compute the average of a series of numbers entered by the user. • To make the program general, it should work with any size set of numbers. • We don‟t need to keep track of each number entered, we only need know the running sum and how many numbers have been added

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review We've run into some of these things before! -A series of numbers could be handled by some sort of loop.If there are n numbers,the loop should execute n times. We need a running sum.This will use an accumulator. 6
6 For Loops: A Quick Review • We‟ve run into some of these things before! – A series of numbers could be handled by some sort of loop. If there are n numbers, the loop should execute n times. – We need a running sum. This will use an accumulator

上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review Input the count of the numbers,n 。Initialize sum to 0 ·Loop n times Input a number,x -Add x to sum Output average as sum/n 7
7 For Loops: A Quick Review • Input the count of the numbers, n • Initialize sum to 0 • Loop n times – Input a number, x – Add x to sum • Output average as sum/n

上游充通大学 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review averagel.py 井 A program to average a set of numbers 井 Illustrates counted loop with accumulator def main(): n input ("How many numbers do you have?" sum 0.0 for i in range(n): x input ("Enter a number >" sumsum x print "\nThe average of the numbers is",sum/n Note that sum is initialized to 0.0 so that sum/n returns a float! 8
8 For Loops: A Quick Review # average1.py # A program to average a set of numbers # Illustrates counted loop with accumulator def main(): n = input("How many numbers do you have? ") sum = 0.0 for i in range(n): x = input("Enter a number >> ") sum = sum + x print "\nThe average of the numbers is", sum/n • Note that sum is initialized to 0.0 so that sum/n returns a float!

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review How many numbers do you have?5 Enter a number >32 Enter a number >45 Enter a number >34 Enter a number >76 Enter a number >45 The average of the numbers is 46.4 9
9 For Loops: A Quick Review How many numbers do you have? 5 Enter a number >> 32 Enter a number >> 45 Enter a number >> 34 Enter a number >> 76 Enter a number >> 45 The average of the numbers is 46.4

上游充通大学 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Remember break and continue? continue for i in[1,2,3,4,5,6,7,8,9,10]: more items in no if i ==5: break yes print i =next item for i in[1,2,3,4,5,6,7,8,9,10]: if i==5: continue break print i 10
Remember break and continue? for i in [1,2,3,4,5,6,7,8,9,10]: if i == 5: break print i for i in [1,2,3,4,5,6,7,8,9,10]: if i == 5: continue print i 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture04 MODULAR PROGRAMMING Functions.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture03 CONDITIONALS AND SEQUENCES Strings, lists and file objects.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture02.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture01.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料_往年试卷.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料_管程.docx
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java学习笔记(JAVA的面向对象编程——课堂笔记).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)JAVA多线程编程详解(详细操作例子).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java多线程编程.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java多线程应用实例(制作烟花效果).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java Primer.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec5.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec4.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec3.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec2.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec1.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java Introduction.docx
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-thread.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-OPERATING SYSTEMS(LINUX SYSTEM CALLS).pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-semaphore EXERCISES.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture06 OBJECTS AND GRAPHICS GUI.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture07 OBJECT ORIENTED DEVELOPMENT Class and Object.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture08 DATA COLLECTION Massive data representation and processing.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture09 SIMULATION AND DESIGN Real-world problem.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture10 ALGORITHM DESIGN AND ANALYSIS To the Classic.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Something You Should Know.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(学习资料)C++练习(题目).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(学习资料)C++练习(答案).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(学习资料)基于MFC的对话框程序.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(课件讲稿)总复习(共八讲).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第1讲 C++语言概述及数据类型(何其昌).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第2讲 C++程序的流程控制.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第3讲 函数与结构化程序设计.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第4讲 数组与结构.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第5讲 指针与引用.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第6讲 C++类(1/2).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第7讲 C++类(2/2).pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲稿)第8讲 Windows应用程序设计.pdf
- 上海交通大学:《C++程序设计与实践》课程教学资源(讲义)方波生成器项目报告书.doc
- 《中文信息学报》:中文组织机构名称与简称的识别.pdf