SlideServe
Menu
Préférences
© SlideServe · v2026

Lab 3

barth avatar
9 diapositives · 337 Views · Sep 06, 2014

Lab 3. Basic Robot Brain. Programs consist of import lines, function definitions and and a starting point. Line 1: from myro import * Line 2: initialize("comX") Line 3: <any other imports> Line 4: <function definitions> Line 5: def main(): Line 6: <do something>

Lab 3 - slide 1
01 / 9Lab 3
1 / 17

Télécharger la présentation

Lab 3
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. Lab 3 Intro to Robots

  2. Basic Robot Brain • Programs consist of import lines, function definitions and and a starting point. Line 1: from myro import * Line 2: initialize("comX") Line 3: <any other imports> Line 4: <function definitions> Line 5: def main(): Line 6: <do something> Line 7: <do something> Line 8: ... Line 9: main() Always start from a function called main() Intro to Robots

  3. Exercise • Combine the following functions into a program that yoyos and wiggles for a bit. Try to make your dance short and sweet. def yoyo(speed, waitTime): forward(speed, waitTime) backward(speed, waitTime) stop() def wiggle(speed, waitTime): motors(-speed, speed) wait(waitTime) motors(speed, -speed) wait(waitTime) stop() Intro to Robots

  4. Exercise: • Take the previous program and rename the main() function dance(). • Now create a main() function that calls dance(). • The problem is if you want to “dance all night”. • Call the program dance.py. • It works but not very interesting: def main(): dance() dance() dance() dance() . . . Intro to Robots

  5. Repetition in Python (and other languages): • Programming languages have syntax for repeating the same thing over and over and over and over . . . • for-loop: when you know exactly how many times to repeat something • while-loop: when you don’t know, in advance, exactly how many times you need to repeat something def main(): for i in range(1): dance() keeps calling dance()until 10 seconds havepassed def main(): while timeRemaining(10): dance() Intro to Robots

  6. Exercise: • Modify the dance.py program to use a for-loop. Intro to Robots

  7. While-Loops: • c(i) is a boolean condition that is True of False depending on the value of i. • f() gives a new value to i, once for each iteration through the loop. • This loop will go on forever if f() never returns a value for which c(i) == False. i = 0 while c(i): # do something i = f() answer = ‘No’while answer == ‘No’: dance() answer = ask(‘Again?’) what happens if you never enter ‘No’but rather ‘no’, ‘n’, NO’, etc. Intro to Robots

  8. Infinite Loops: • The example on the last slide shows that a loop can, by accident, go on forever. You can also intentionally write an infinite loop. • How do you stop this loop? while True: # do something Ctrl-C Intro to Robots

  9. Exercise: • Modify the dance.py program to replace the for-loop with a while-loop. • Try various while-loops including an infinite loop. Intro to Robots