SlideServe
Menu
Préférences
© SlideServe · v2026

ITEC 109

kort avatar
12 diapositives · 323 Views · Jul 31, 2014

ITEC 109. Lecture 5 Algorithms / Programming. Review. Introduction to python What are variables? What are functions? How do you make a turtle move forward one spot?. Objectives. Difference between algorithms and programs Solve a simple problem with python Learn how to write functions.

ITEC 109 - slide 1
01 / 12ITEC 109
1 / 17

Télécharger la présentation

ITEC 109
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. ITEC 109 Lecture 5 Algorithms / Programming

  2. Review • Introduction to python • What are variables? • What are functions? • How do you make a turtle move forward one spot?

  3. Objectives • Difference between algorithms and programs • Solve a simple problem with python • Learn how to write functions

  4. Difference Algorithm 3 eggs 1 cup water 1/3 cup vegetable oil 1 box cake mix Mix ingredients Pour in pan Put pan in oven at 350 for 30 minutes Program Algorithms tell you what to do to accomplish something Programs are the result of following an algorithm’s instructions

  5. Problem • I want my newspaper • It is in a box outside • It is too hot • Solution • I have a turtle go get it for me

  6. Algorithm • Go down 2 places • Pick up paper (Put the pen down) • Move up two spaces • Drop paper

  7. Program • Turtle limitations • Adapt tools to match algorithm • Task: move down one space • Capabilities: turn left and move forward • Animation: • Add import time at beginning of program • Insert time.sleep(.5) after each movement • Solution: ?

  8. Solution Algorithm Program Go down 2 spaces Pick up paper Move up two spaces Drop paper w = world(); t = turtle(w); turnLeft(t); turnLeft(t); forward(t); forward(t); penDown(t); turnLeft(t); turnLeft(t); forward(t); forward(t); penUp(t);

  9. Functions turnLeft(t); turnLeft(t); turnLeft(t); • Why not just have the • idea of turn right that • uses 3 turn lefts? Turning right Create Once def right(t): turnLeft(t); turnLeft(t); turnLeft(t); right(t); right(t); Use many times

  10. Pro/Con Algorithm Go down 2 spaces Pick up paper Move up two spaces Drop paper • Pro • Hide details • Maps to algorithm • Con • Overhead • Requires thought / planning Program turn (t); move(t); penDown(t) turn(t); move(t); penUp(t) Functions defturnAround(t): turnLeft(t); turnLeft(t); def move(t): forward(t); forward(t);

  11. Functions With Without w = world(); t = turtle(w); def around(t): turnLeft(t); turnLeft(t); def move(t): forward(t); forward(t); around(t); move(t); penDown(t); around(t); move(t); penUp(t); w = world(); t = turtle(w); turnLeft(t); turnLeft(t); forward(t); forward(t); penDown(t); turnLeft(t); turnLeft(t); forward(t); forward(t); penUp(t); Overhead for simple tasks Beneficial for large tasks

  12. Homework • Write a program that uses functions • Demo on Wed. for credit • All turtle HW assignments count as one homework assignment grade