Skip to content
Snippets Groups Projects
Commit 1171d925 authored by mathieu.schiess@edu.ge.ch's avatar mathieu.schiess@edu.ge.ch
Browse files

Ajout d'une première version du notebook sur les fonctions avec paramètres

parent b3f3674d
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
### Paquebot
Une boucle `for` est utilisée dans l'exemple suivant pour dessiner les hublots d'un paquebot. Les hublots sont numérotés en utilisant la variable `i`.
%% Cell type:markdown id: tags:
<h3 style="color:teal;background-color:azure;" > <i class="fa fa-pencil" aria-hidden="true"> </i> &nbsp; Exercice 8 </h3>
Créez une fonction `paquebot()` et dessinez-en un deuxième.
%% Cell type:code id: tags:
``` python
from turtle import *
forward(200)
left(80)
forward(60)
left(100)
forward(220)
left(100)
forward(60)
up()
left(125)
forward(40)
right(45)
for i in range(6):
dot(20, 'lightgray')
write(i, font=('Arial', 20, 'normal'))
forward(30)
done()
```
%% Output
%% Cell type:markdown id: tags:
<h3 style="color:teal;background-color:azure;" > <i class="fa fa-pencil" aria-hidden="true"> </i> &nbsp; Exercice 12</h3>
Avec des rails de chemin de fer, dessinez un circuit en forme d'un rond (deux rails avec les traverses).
![rails](https://raw.githubusercontent.com/edunumsec2/book/master/src/appr/prog1/media/rails2.png)
Utilisez une boucle `for` pour la répétition des traverses.
%% Cell type:code id: tags:
``` python
from turtle import *
# Prénom Nom, classe
def traverse():
...
forward(200)
done()
```
%% Cell type:markdown id: tags:
<h3 style="color:teal;background-color:azure;" > <i class="fa fa-pencil" aria-hidden="true"> </i> &nbsp; Exercice 12</h3>
Triangle de sierpinski
%% Cell type:code id: tags:
``` python
from turtle import *
def triangle(l):
for _ in range(3):
forward(l)
left(120)
x = 50
for _ in range(3):
triangle(x)
forward(x)
right(120)
done()
```
%% Output
%% Cell type:code id: tags:
``` python
# A corriger !!!
from turtle import *
def triangle(l):
for _ in range(3):
forward(l)
left(120)
def sierpinski(l, n):
if n ==0:
triangle(l)
else:
for _ in range(3):
sierpinski(l/3, n-1)
forward(l/3)
right(120)
speed(10)
sierpinski(200, 1)
done()
```
%% Output
%% Cell type:markdown id: tags:
<h3 style="color:teal;background-color:azure;" > <i class="fa fa-pencil" aria-hidden="true"> </i> &nbsp; Exercice 13 : Pingpong</h3>
La fonction `pingpong()` reprend le dessin de la leçon 1 et ajoute trois paramètres
%% Cell type:code id: tags:
``` python
from turtle import *
def pingpong(d, c, c2):
down()
left(90)
color(c) # poignée
width(d/8)
forward(d/2)
color(c2) # plaque
width(d/2)
forward(d/10)
up() # retourner au point de départ
backward(6/10*d)
right(90)
pingpong(200, 'brown', 'red')
forward(100)
pingpong(150, 'brown', 'blue')
done()
```
......
Notebooks/imgs_chap4/ex_carres_couleur.png

6.06 KiB

Notebooks/imgs_chap4/ex_triangles.png

3 KiB

Notebooks/imgs_chap4/mondrian.jpg

45.9 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment