0% ont trouvé ce document utile (0 vote)
72 vues22 pages

Matrix Python

Transféré par

Jean-Aymeric Diet
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PPTX, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
72 vues22 pages

Matrix Python

Transféré par

Jean-Aymeric Diet
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PPTX, PDF, TXT ou lisez en ligne sur Scribd

MATRIX PYTHON

jeanaymeric@[Link] 1
MATRIX PYTHON
L’objectif est :
• de se familiariser avec le langage Python
• de manipuler les différentes structures répétitives
• de manipuler des variables simples et des tableaux
• d’importer des bibliothèques
• de jouer un peu avec la console
• de faire un programme qui permet de se la péter un peu

jeanaymeric@[Link] 2
PREMIÈRE ÉTAPE
Fabriquer un programme qui :
• Demande un nombre à l’utilisateur
• Décompte à partir de ce nombre jusqu’à 0
Entrez un nombre : 15
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0

jeanaymeric@[Link] 3
PREMIÈRE ÉTAPE - SOLUTION
number = int(input ("Entrez un nombre : "))

for i in range(number, -1, -1):


print(i)
print(' ')

jeanaymeric@[Link] 4
DEUXIÈME ÉTAPE
Fabriquer un programme qui :
• Créé un tableau de 10 cases
• Remplit chacune des cases du tableau avec son indice
• Affiche le contenu du tableau

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

jeanaymeric@[Link] 5
DEUXIÈME ÉTAPE - SOLUTION
digitList = []
for i in range(10):
[Link](i)
print(digitList)

jeanaymeric@[Link] 6
TROISIÈME ÉTAPE
Modifier le programme précédent pour qu’il :
• Affiche chaque valeur du tableau sur une seule ligne sans séparateur
• Décrémente chaque valeur du tableau de 1
• Lorsque la valeur arrive à 0, la valeur du tableau est remplacée par un
espace
• Affiche le contenu du tableau
0123456789
[' ', 0, 1, 2, 3, 4, 5, 6, 7, 8]

jeanaymeric@[Link] 7
TROISIÈME ÉTAPE - SOLUTION
digitList = []

for i in range(0, 10):


[Link](i)

for i in range(len(digitList)) :
if digitList[i] == 0:
digitList[i] = " "
else:
digitList[i] -= 1
print(digitList)

jeanaymeric@[Link] 8
QUATRIÈME ÉTAPE
Modifier le programme précédent pour qu’il :
• Affiche chaque valeur du tableau sur une seule ligne sans séparateur
et décrémente chaque valeur du tableau de 1, 10 fois
0123456789
012345678
01234567
0123456
012345
01234
0123
012
01
0

jeanaymeric@[Link] 9
QUATRIÈME ÉTAPE - SOLUTION
digitList = []

for i in range(0, 10):


[Link](i)

for j in range(10):
for i in range(len(digitList)):
print(digitList[i], end="")
if digitList[i] == 0 or digitList[i] == " ":
digitList[i] = " "
else:
digitList[i] -= 1
print()
print(digitList)

jeanaymeric@[Link] 10
CINQUIÈME ÉTAPE
Modifier le programme précédent pour qu’il n’exécute pas sa boucle 10
fois, mais qu’il contrôle qu’au moins un nombre a été décrémenté
0123456789
012345678
01234567
0123456
012345
01234
0123
012
01
0

jeanaymeric@[Link] 11
CINQUIÈME
digitList = []
ÉTAPE - SOLUTION
for i in range(0, 10):
[Link](i)

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(digitList[i], end="")
if digitList[i] == 0 or digitList[i] == " ":
digitList[i] = " "
else:
digitList[i] -= 1
atLeastOne = True
print()
print(digitList)

jeanaymeric@[Link] 12
SIXIÈME ÉTAPE
Modifier le programme précédent pour que :
• Le tableau fasse désormais 100 cases au lieu de 10
• Soit rempli avec des valeurs aléatoires entre 0 et 9
5247665472535486449625585841028379092971904394819722176684462068774789263507771963689276925939530436
4136554361424375338514474730 17268 818608 3283708611065573351 576636781524 666085257816581482842 325
302544325031326422740336362 06157 7075 7 21726 7500 54462240 465525670413 555 74146705470371731 214
2 1433214 2021531163 225251 5046 6 64 6 10615 64 4335113 35441456 302 444 630356 436 260620 103
1 0322103 1 10420052 114140 4 35 5 53 5 0 504 53 3224002 24330345 2 1 333 52 245 325 15 51 0 2
0 2110 2 0 0 31 41 00303 3 24 4 42 4 4 3 42 2113 1 1322 234 1 0 222 41 134 214 04 40 1
100 1 20 30 2 2 2 13 3 31 3 3 2 31 1002 0 0211 123 0 111 30 023 103 3 3 0
0 0 1 2 1 1 1 02 2 20 2 2 1 20 0 1 100 012 000 2 12 0 2 2 2
0 1 0 0 0 1 1 1 1 1 0 1 0 0 01 1 01 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0

jeanaymeric@[Link] 13
SIXIÈME ÉTAPE - SOLUTION
import random

digitList = []
for i in range(0, 100):
[Link]([Link](0, 9))

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(digitList[i], end="")
if digitList[i] == 0 or digitList[i] == " ":
digitList[i] = " "
else:
digitList[i] -= 1
atLeastOne = True
print()

jeanaymeric@[Link] 14
SEPTIÈME ÉTAPE
Modifier le programme précédent pour que :
• Le tableau soit rempli avec des valeurs aléatoires entre 32 et 255 (un
caractère affichable)
• Il affiche le caractère correspondant à sa valeur ascii - chr()
• Il s’arrête au caractère 32 au lieu de 0
6) K $ J Q * I R A E M F & 8Q? T/T : 6C D R DL! 2
5( J # I P ) H Q @ D L E % 7P> S.S 9 5B C Q CK 1
4' I " H O ( G P ? C K D $ 6O= R-R 8 4A B P BJ 0
3& H ! G N ' F O > B J C # 5N< Q,Q 7 3@ A O AI /
2% G F M & E N = A I B " 4M; P+P 6 2? @ N @H .
1$ F E L % D M < @ H A ! 3L: O*O 5 1> ? M ?G -
0# E D K $ C L ; ? G @ 2K9 N)N 4 0= > L >F ,
/" D C J # B K : > F ? 1J8 M(M 3 /< = K =E +
.! C B I " A J 9 = E > 0I7 L'L 2 .; < J <D *
- B A H ! @ I 8 < D = /H6 K&K 1 -: ; I ;C )
jeanaymeric@[Link] 15
SEPTIÈME ÉTAPE - SOLUTION
import random

digitList = []
for i in range(0, 100):
[Link]([Link](32, 255))

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(chr(digitList[i]), end="")
if digitList[i] > 32:
digitList[i] -= 1
atLeastOne = True
print()

jeanaymeric@[Link] 16
HUITIÈME ÉTAPE
Modifier le programme précédent en ajoutant un petit délai entre
chaque affichage et augmentez la largeur d’affichage.
Il faut :
• Importer la bibliothèque time
• Utiliser la fonction [Link](0.02)
d o-=Ju 5 '` t Oc 0?% ii k^j ,N S F;WU v 3 , z Z e Z a<z a# j } A @ & y w' > uUt / sr9: +5K zb }O BPPqD ?7 uV K _ T = [V 5 }l T#w
z n
c n,<It 4 &_ s Nb />$ hh j]i +M R E:VT u 2 + y Y d Y `;y `" i | @ ? % x v& = tTs . rq89 *4J ya |N AOOpC >6 tU J ^ S < ZU 4 |k S"v
y m
b m+;Hs 3 %^ r Ma .=# gg i\h *L Q D9US t 1 * x X c X _:x _! h { ? > $ w u% < sSr - qp78 )3I x` {M @NNoB =5 sT I ] R ; YT 3 {j R!u
x l
a l*:Gr 2 $] q L` -<" ff h[g )K P C8TR s 0 ) w W b W ^9w ^ g z > = # v t$ ; rRq , po67 (2H w_ zL ?MMnA <4 rS H \ Q : XS 2 zi Q t
w k
` k)9Fq 1 #\ p K_ ,;! ee gZf (J O B7SQ r / ( v V a V ]8v ] f y = < " u s# : qQp + on56 '1G v^ yK >LLm@ ;3 qR G [ P 9 WR 1 yh P s
v j
_ j(8Ep 0 "[ o J^ +: dd fYe 'I N A6RP q . ' u U ` U \7u \ e x < ; ! t r" 9 pPo * nm45 &0F u] xJ =KKl? :2 pQ F Z O 8 VQ 0 xg O r
u i
^ i'7Do / !Z n I] *9 cc eXd &H M @5QO p - & t T _ T [6t [ d w ; : s q! 8 oOn ) ml34 %/E t\ wI <JJk> 91 oP E Y N 7 UP / wf N q
t h
] h&6Cn . Y m H\ )8 bb dWc %G L ?4PN o , % s S ^ S Z5s Z c v : 9 r p 7 nNm ( lk23 $.D s[ vH ;IIj= 80 nO D X M 6 TO . ve M p
s g
\ g%5Bm - X l G[ (7 aa cVb $F K >3OM n + $ r R ] R Y4r Y b u 9 8 q o 6 mMl ' kj12 #-C rZ uG :HHi< 7/ mN C W L 5 SN - ud L o
r f
[ jeanaymeric@[Link]
f$4Al , W k FZ '6 `` bUa #E J =2NL m * # q Q \ Q X3q X a t 8 7 p n 5 lLk & ji01 ",B qY tF 9GGh; 6. lM B V K 4 RM , 17
tc K n
q e
HUITIÈME
import random
ÉTAPE - SOLUTION
import time

digitList = []
for i in range(0, 198):
[Link]([Link](32, 255))

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(chr(digitList[i]), end="")
if digitList[i] > 32:
digitList[i] -= 1
atLeastOne = True
print()
[Link](0.02)

jeanaymeric@[Link] 18
NEUVIÈME ÉTAPE
• Il ne reste plus qu’à tout mettre en vert et c’est pratiquement fini
C # E M D/ 4&! 5 G $ V*4 N!F P >Q + 3 P F( ? > *= - 2 (%A <!6# P F 6 C P B D3 K& G WSL9 + - , " * O 4 P9 =P @" U
B " D L C. 3% 4 F # U)3 M E O =P * 2 O E' > = )< , 1 '$@ ; 5" O E 5 B O A C2 J% F VRK8 * , + ! ) N 3 O8 <O ?! T
A ! C K B- 2$ 3 E " T(2 L D N <O ) 1 N D& = < (; + 0 &#? : 4! N D 4 A N @ B1 I$ E UQJ7 ) + * ( M 2 N7 ;N > S
@ B J A, 1# 2 D ! S'1 K C M ;N ( 0 M C% < ; ': * / %"> 9 3 M C 3 @ M ? A0 H# D TPI6 ( * ) ' L 1 M6 :M = R
? A I @+ 0" 1 C R&0 J B L :M ' / L B$ ; : &9 ) . $!= 8 2 L B 2 ? L > @/ G" C SOH5 ' ) ( & K 0 L5 9L < Q
> @ H ?* /! 0 B Q%/ I A K 9L & . K A# : 9 %8 ( - # < 7 1 K A 1 > K = ?. F! B RNG4 & ( ' % J / K4 8K ; P
= ? G >) . / A P$. H @ J 8K % - J @" 9 8 $7 ' , " ; 6 0 J @ 0 = J < >- E A QMF3 % ' & $ I . J3 7J : O
< > F =( - . @ O#- G ? I 7J $ , I ?! 8 7 #6 & + ! : 5 / I ? / < I ; =, D @ PLE2 $ & % # H - I2 6I 9 N
; = E <' , - ? N", F > H 6I # + H > 7 6 "5 % * 9 4 . H > . ; H : <+ C ? OKD1 # % $ " G , H1 5H 8 M
: < D ;& + , > M!+ E = G 5H " * G = 6 5 !4 $ ) 8 3 - G = - : G 9 ;* B > NJC0 " $ # ! F + G0 4G 7 L
9 ; C :% * + = L * D < F 4G ! ) F < 5 4 3 # ( 7 2 , F < , 9 F 8 :) A = MIB/ ! # " E * F/ 3F 6 K
8 : B 9$ ) * < K ) C ; E 3F ( E ; 4 3 2 " ' 6 1 + E ; + 8 E 7 9( @ < LHA. " ! D ) E. 2E 5 J
7 9 A 8# ( ) ; J ( B : D 2E ' D : 3 2 1 ! & 5 0 * D : * 7 D 6 8' ? ; KG@- ! C ( D- 1D 4 I
6 8 @ 7" ' ( : I ' A 9 C 1D & C 9 2 1 0 % 4 / ) C 9 ) 6 C 5 7& > : JF?, B ' C, 0C 3 H
5 7 ? 6! & ' 9 H & @ 8 B 0C % B 8 1 0 / $ 3 . ( B 8 ( 5 B 4 6% = 9 IE>+ A & B+ /B 2 G
4 6 > 5 % & 8 G % ? 7 A /B $ A 7 0 / . # 2 - ' A 7 ' 4 A 3 5$ < 8 HD=* @ % A* .A 1 F
3 5 = 4 $ % 7 F $ > 6 @ .A # @ 6 / . - " 1 , & @ 6 & 3 @ 2 4# ; 7 GC<) ? $ @) -@ 0 E
2 4 < 3 # $ 6 E # = 5 ? -@ " ? 5 . - , ! 0 + % ? 5 % 2 ? 1 3" : 6 FB;( > # ?( ,? / D
1 3 ; 2 " # 5 D " < 4 > ,? ! > 4 - , + / * $ > 4 $ 1 > 0 2! 9 5 EA:' = " >' +> . C
0 2 : 1 ! " 4 C ! ; 3 = +> = 3 , + * . ) # = 3 # 0 = / 1 8 4 D@9& < ! =& *= - B
/ 1 9 0 ! 3 B : 2 < *= < 2 + * ) - ( " < 2 " / < . 0 7 3 C?8% ; <% )< , A
. 0 8 / 2 A 9 1 ; )< ; 1 * ) ( , ' ! ; 1 ! . ; - / 6 2 B>7$ : ;$ (; + @
- / 7 . 1 @ 8 0 : (; : 0 ) ( ' + & : 0 - : , . 5 1 A=6# 9 :# ': * ?
, . 6 - 0 ? 7 / 9 ': 9 / ( ' & * % 9 / , 9 + - 4 0 @<5" 8 9" &9 ) >

jeanaymeric@[Link] 19
NEUVIÈME ÉTAPE - SOLUTION
import random
import time
import os

[Link]("color 02")
digitList = []
for i in range(0, 198):
[Link]([Link](32, 200))

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(chr(digitList[i]), end="")
if digitList[i] > 32:
digitList[i] -= 1
atLeastOne = True
print()
[Link](0.02)

jeanaymeric@[Link] 20
DIXIÈME ÉTAPE
• Trouvez une solution pour que le programme ne s’arrête jamais.

• L’idée d’une boucle infinie autour de tout notre programme est à


proscrire. Bien sûr le programme ne s’arrêterai jamais, mais ce n’est
pas cela que l’on veut. Des lignes de caractères à la Matrix doivent
toujours être présentes à l’écran.

jeanaymeric@[Link] 21
DIXIÈME ÉTAPE - SOLUTION
import random
import time
import os

[Link]("color 02")
digitList = []
for i in range(0, 198):
[Link]([Link](32, 150))

atLeastOne = True
while atLeastOne:
atLeastOne = False
for i in range(len(digitList)):
print(chr(digitList[i]), end="")
if digitList[i] > 32:
digitList[i] -= 1
atLeastOne = True
elif [Link](0, 200) == 0:
digitList[i] = [Link](32, 150);
atLeastOne = True
print()
[Link](0.02)

jeanaymeric@[Link] 22

Vous aimerez peut-être aussi