ΜΥΥ105: Εισαγωγή στον Προγραμματισμό Έλεγχος Ροής Προγράμματος
Ροή προγράμματος Μέχρι τώρα έχουμε δει προγράμματα απλής ροής Οι εντολές εκτελούνται η μία μετά την άλλη σύμφωνα με την ακολουθία γραμμών του προγράμματος Σε τυπικά προγράμματα η ροή είναι πιο σύνθετη Χρήση συνθηκών για τον έλεγχο ροής Χρήση επαναλήψεων 2
Διαγράμματα Ροής Τρόπος σχεδιασμού ενός αλγορίθμου Ο αλγόριθμος είναι το σχέδιο του προγράμματος Βασικά μέρη ενός διαγράμματος ροής temp > 30 o prit(x) yes απόφαση εντολή ροή 3
Έλεγχος ροής με if if δυαδική έκφραση : μπλοκ εντολών temp = eval(iput('eter the curret temperature: ')) if temp > 25: prit('it is hot!') prit('be sure to drik liquids.') οι εντολές στο µπλοκ είναι στοιχισµένες 4 θέσεις δεξιά temp = eval(iput('eter the curret temperature: ')) temp > 25 o yes prit('it is hot!') prit('be sure to drik liquids.') 4
Έλεγχος ροής με if if δυαδική έκφραση : μπλοκ εντολών temp = eval(iput('eter the curret temperature: ')) if temp > 25: prit('it is hot!') prit('be sure to drik liquids.') prit('goodbye!') temp = eval(iput('eter the curret temperature: ')) µετά / έξω από το µπλόκ, εκτελείται ανεξάρτητα από την έκβαση της if temp > 25 o yes prit('it is hot!') prit('be sure to drik liquids.') prit('goodbye!') 5
Έλεγχος ροής με if/else if δυαδική έκφραση : μπλοκ εντολών else : μπλοκ εντολών temp = eval(iput('eter the curret temperature: ')) if temp > 25: prit('it is hot!') prit('be sure to drik liquids.') prit('it is ot hot.') prit('brig a jacket.') prit('goodbye!') 6
Έλεγχος ροής με if/else if δυαδική έκφραση : μπλοκ εντολών else : μπλοκ εντολών temp = eval(iput('eter the curret temperature: ')) o prit('it is ot hot.') prit('brig a jacket.') temp > 25 yes prit('it is hot!') prit('be sure to drik liquids.') prit('goodbye!') 7
Έλεγχος ροής με if/elif/else if δυαδική έκφραση : μπλοκ εντολών elif δυαδική έκφραση : μπλοκ εντολών else : μπλοκ εντολών x = eval(iput('eter a umber: ')) if x > 0: prit('the umber is positive') elif x < 0: prit('the umber is egative') prit('the umber is zero') 8
Έλεγχος ροής με if/elif/else if δυαδική έκφραση : μπλοκ εντολών elif δυαδική έκφραση : μπλοκ εντολών else : μπλοκ εντολών µπορούµε να βάλουµε όσα διαδοχικά elif θέλουµε! Το elif σηµαίνει else if x = eval(iput('eter a umber: ')) yes prit('the umber is egative') x > 0 o x < 0 o yes prit('the umber is positive') prit('the umber is zero') 9
Φωλιασμένα if x = eval(iput('eter a umber: ')) if x > 0: if x > 1000: prit('a large positive umber') else : prit('the umber is positive') elif x < 0: prit('the umber is egative') prit('the umber is zero') x = eval(iput('eter a umber: ')) yes prit('the umber is egative') x > 0 o x < 0 o yes yes x > 1000 o prit('a large positive umber') prit('the umber is positive') prit('the umber is zero') 10
Συγκρίσεις >>> a = 3 >>> 0<a<5 True >>> b = 3 >>> a ==b True >>> a is b True >>> s1 = 'hello' >>> s2 = 'hello' >>> s1 is s2 True >>> x = [1,2,3] >>> y = [1,2,3] >>> x == y True >>> x is y False >>> z = x >>> z is x True Για λίστες, πλειάδες, λεξικά, True µόνο αν οι µεταβλητές δείχνουν στο ίδιο αντικείµενο Το == ελέγχει αν οι λίστες έχουν τα ίδια στοιχεία. 11
Τελεστές σύγκρισης x == y το x ισούται με το y. x < y το x είναι μικρότερο από το y. x > y το x είναι μεγαλύτερο από το y. x >= y το x είναι μεγαλύτερο ή ίσο από το y. x <= y το x είναι μικρότερο ή ίσο από το y. x!= y το x είναι διάφορο του y. x is y x και y είναι το ίδιο αντικείμενο. x is ot y x και y είναι διαφορετικά αντικείμενα. x i y το x είναι μέλος της ακολουθίας y. x ot i y το x δεν είναι μέλος της ακολουθίας y. 12
Λεξικογραφική σειρά >>> "alpha" < "beta" True >>> [1, 2] < [2, 1] True >>> [2, [1, 4]] < [2, [1, 5]] True Σαν να συγκρίνουµε το 12 µε το 21 ή το 214 µε το 215 >>> [2,1,4] < ['a','b',1] Traceback (most recet call last): File "<pyshell#16>", lie 1, i <module> [2,1,4] < ['a','b',1] TypeError: uorderable types: it() < str() 13
Παραδείγματα ame = iput('what is your ame? ') if 's' i ame: prit('your ame cotais the letter "s".') prit('your ame does ot cotai the letter "s".') 14
Παραδείγματα umber = it(iput('eter a umber betwee 1 ad 10: ')) if umber <= 10: if umber >= 1: prit('great!') prit('wrog!') prit('wrog!') umber = it(iput('eter a umber betwee 1 ad 10: ')) if 1<=umber<=10: prit('great!') prit('wrog!') 15
Παραδείγματα (ad, or, ot) umber = it(iput('eter a umber betwee 1 ad 10: ')) if umber>=1 ad umber<=10: prit('great!') prit('wrog!') umber = it(iput('eter a umber betwee 1 ad 10: ')) if umber<1 or umber>10: prit('wrog!') prit('great!') umber = it(iput('eter a umber betwee 1 ad 10: ')) if umber>=1 ad ot umber>10: prit('great!') prit('wrog!') 16
Εναλλακτικοί τρόποι χρήσης if >>> ame = iput('please eter your ame: ') or '<ukow>' Please eter your ame: >>> ame '<ukow>' >>> ame = iput('please eter your ame: ') or '<ukow>' Please eter your ame: Joe >>> ame 'Joe' επιστρέφει το or µόνο αν το iput() είναι κενό (False) False είναι τα ακόλουθα: 0,'', (), {}, [], Noe >>> y=0 >>> x = 5 if y>1 else 0 >>> x 0 το ίδιο µε: if y>1: x=5 x=0 Γράψτε μια γραμμή κώδικα που βρίσκει τον μέγιστο δύο αριθμών (χωρίς χρήση max) 17
Άσκηση Τί πρόβλημα έχει ο παρακάτω κώδικας; temp = it(iput('please iput the curret temperature: ')) if temp > 5: prit('it is cool') elif temp > 25: prit('it is hot!') prit('it is freezig!') Πώς θα φτιάξουμε το πρόγραμμά μας; δεν θα εκτελεστεί ποτε! πρέπει να προσέχουµε τη σειρά µε την οποία βάζουµε τις συνθήκες!... if temp > 25: prit('it is hot!') elif temp > 5: prit('it is cool')... 18
Άσκηση Είσοδος: ένας αριθμός από το 1 ως το 4 Έξοδος: τύπωσε Joh αν είναι 1, Paul αν είναι 2, Maria αν είναι 3, Sue αν είναι 4 c = it(iput('please iput a umber betwee 1 ad 4: ')) if c == 1: prit('joh') elif c == 2: prit('paul') elif c == 3: prit('maria') elif c == 4: prit('sue') 19
Άσκηση Είσοδος: ένας αριθμός από το 1 ως το 4 Έξοδος: τύπωσε Joh αν είναι 1, Paul αν είναι 2, Maria αν είναι 3, Sue αν είναι 4 Χωρίς να χρησιμοποιήσετε if! d = {1:'Joh',2:'Paul',3:'Maria',4:'Sue'} c = it(iput('please iput a umber betwee 1 ad 4: ')) prit(d[c]) 20
Επαναλήψεις (loops) Στα προγράμματά μας μπορεί να θέλουμε να τρέξουμε την ίδια σειρά εντολών πολλές φορές με πιθανές παραμετροποιημένες μικροαλλαγές σε κάθε επανάληψη είτε γνωρίζοντας πόσες φορές (for) είτε ενόσω μια συνθήκη είναι αληθής (while) o x = 1 x <= 100 yes prit(x) x = x + 1 21
Ο βρόγχος while Eνόσω η συνθήκη είναι αληθής εκτελείται το μπλοκ εντολών που είναι στοιχισμένες κάτω της x =1 while x <= 100: prit(x) x += 1 κενό αλφαριθµητικό ame = '' while ot ame: ame = iput('please eter your ame: ') prit('hello, %s!' % ame) True, αν το ame είναι κενό Αν θέλουµε το διάστηµα ' ' να µην είναι αποδεκτό σαν όνοµα, βάζουµε: while ot ame or ame.isspace() ή while ot ame.strip() 22
Ο βρόγχος for Χρησιμοποιείται αν θέλουμε να επαναλάβουμε ένα μπλοκ κώδικα, για κάθε τιμή ενός συνόλου umbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for x i umbers: prit(x) words = ['this', 'is', 'a', 'ex', 'parrot'] for x i words: prit(x[-1]) for x i rage(1,101): prit(x) ορίζει ένα διάστηµα τιµών, όπως και στο slicig 23
Άσκηση Γράψτε ένα πρόγραμμα που θα παίρνει ένα αλφαριθμητικό από το χρήστη και θα τυπώνει τα φωνήεντα του s = iput('please iput a strig: ') for c i s: if c i 'aeoiu': prit(c) 24
Άσκηση Τύπωσε τους ζυγούς αριθμούς μέχρι το 10 for x i rage(11): if x % 2 == 0: prit(x) rage(11)=rage(0,11)= [0,1,2,3,4,5,6,7,8,9,10] for x i rage(0,11,2): prit(x) 25
Άσκηση Δίνεται μια λίστα lst αριθμών. Βρές το γινόμενό τους lst = [3,4,6,5] prod = 1 for x i lst: prod *= x prit(prod) το ίδιο µε prod = prod*x Πως θα υπολογίσουμε το!? = eval(iput( : )) prod = 1 for x i rage(): prod *= x+1 prit(prod) 26
Επαναλήψεις σε λεξικά d = {'x': 1, 'y': 2, 'z': 3} for key i d: prit(key, 'correspods to', d[key]) Εναλλακτικά: d = {'x': 1, 'y': 2, 'z': 3} for key, value i d.items(): prit(key, 'correspods to', value) z correspods to 3 y correspods to 2 x correspods to 1 Υπενθύµηση: στα λεξικά δεν υπάρχει συγκεκριµένη σειρά προσπέλασης των ζευγαριών 27
Παράλληλες επαναλήψεις ames = ['ae', 'beth', 'george', 'damo'] ages = [12, 45, 32, 102] for i i rage(le(ames)): prit (ames[i], 'is', ages[i], 'years old') ae is 12 years old beth is 45 years old george is 32 years old damo is 102 years old rage(4)=rage(0,4) Είναι συνηθισµένος αυτός ο τρόπος διάσχισης της λίστας µε βάση τους δείκτες, ξεκινώντας από το µηδέν και πηγαίνοντας µέχρι το le(ames)-1 28
Παράλληλες επαναλήψεις - zip ames = ['ae', 'beth', 'george', 'damo'] ages = [12, 45, 32, 102] for ame, age i zip(ames, ages): prit(ame, 'is', age, 'years old') ae is 12 years old beth is 45 years old george is 32 years old damo is 102 years old ames = ['ae', 'beth', 'george', 'damo', 'paul', 'sue'] ages = [12, 45, 32, 102] for ame, age i zip(ames, ages): prit(ame, 'is', age, 'years old') Τι θα τυπώσει; Το ίδιο! Η zip σταµατά στην κοντύτερη σειρά 29
Παράλληλες επαναλήψεις - zip ames = ['ae', 'beth', 'george', 'damo'] ages = [12, 45, 32, 102] lst = (3,45,66) d = {'1':3, '2':4} for,a,l,di i zip(ames, ages, lst, d): prit(,a,l,d[di]) ae 12 3 4 beth 45 45 3 30
Διακοπή βρόγχου με break Χρησιμοποιούμε break όταν θέλουμε να σταματήσουμε ένα loop Π.χ. βρες το μεγαλύτερο ακέραιο τετράγωνο < 100 from math import sqrt for i rage(99, 0, -1): root = sqrt() if root == it(root): prit () break κατέβασµα από το 99 στο 1 µε αρνητικό βήµα (-1) Π.χ. προσπαθήστε: list(rage(0,100,5)) είναι το root ακέραιος; 31
While True με break Όταν θέλουμε να βγούμε από το βρόγχο ακριβώς μόλις ικανοποιηθεί μια συνθήκη εξόδου, χωρίς να εκτελέσουμε τις υπόλοιπες εντολές του βρόγχου πάντα αληθές while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") µόνη πιθανή έξοδος 32
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") 33
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 34
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 x == 40 35
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! x == 40 36
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! x == 40 37
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 x == 40 38
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 x == 60 39
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! x == 60 40
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! x == 60 41
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! How may states are i the U.S.? 50 x == 60 42
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! How may states are i the U.S.? 50 x == 50 43
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! How may states are i the U.S.? 50 Correct! There are 50 states i the U.S. x == 50 44
While True με break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") How may states are i the U.S.? 40 The aswer is NOT correct! How may states are i the U.S.? 60 The aswer is NOT correct! How may states are i the U.S.? 50 Correct! There are 50 states i the U.S. x == 50 45
Άσκηση Ποια η διαφορά μεταξύ των 2 προγραμμάτων; while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") 46
Άσκηση Γράψε το ίδιο πρόγραμμα χωρίς χρήση break while True: x = it(iput("how may states are i the U.S.? ")) if x == 50: prit("correct! There are 50 states i the U.S.") break prit("the aswer is NOT correct!") x = 0 while x!=50 : x = it(iput("how may states are i the U.S.? ")) if x!=50 : prit("the aswer is NOT correct!") prit("correct! There are 50 states i the U.S.") 47
Άσκηση Δίνεται μια λίστα lst. Γράψε ένα πρόγραμμα που να ελέγχει αν η λίστα είναι ταξινομημένη lst = [3,4,6,5,7] is_sorted = True for i i rage(0,le(lst)-1): if lst[i] > lst[i+1]: prit('list',lst, 'is ot sorted!') is_sorted = False break if is_sorted: prit('list', lst, 'is sorted!') βοηθητική µεταβλητή έλεγχος αν υπαρχει στοιχείο µεγαλύτερο από το επόµενο Πολλές φορές είναι πιο εύκολο να βρούμε την συνθήκη που παραβιάζει αυτό που ψάχνουμε αντί γι αυτή που το ικανοποιεί. 48
H break σπάει τον εσωτερικότερο βρόγχο Τι θα τυπώσει το παρακάτω πρόγραμμα; for i i rage(3): for j i rage(3): prit(i,j) if j==1: break 0 0 0 1 1 0 1 1 2 0 2 1 Δες το στο http://pythotutor.com/visualize.html Πως θα τυπώσουμε τα ζευγάρια (i,j) όπου i <= j? 49
Η εντολή cotiue Τερματίζει την τρέχουσα επανάληψη και πάει στην επόμενη Παράδειγμα: τύπωσε όλους τους χαρακτήρες ενός αλφαριθμητικού, εκτός από τα φωνήεντα s = iput('please iput a strig: ') for c i s: if c i 'aeoiu': cotiue prit(c) Πήγαινε κατευθείαν στην επόµενη επανάληψη 50
Χρήση else στη for Εκτελείται μόνο όταν το for-loop δεν έχει «σπάσει» λόγω break from math import sqrt for i rage(99, 90, -1): root = sqrt() if root == it(root): prit () break prit("did't fid it") Εκτελείται αν το for τερµατίσει χωρίς break 51
Άσκηση Γράψετε ένα πρόγραμμα που ελέγχει αν δύο λίστες είναι ίδιες χρησιμοποιώντας την for - else x = [1,2,3] y = [1,2,3] for i,j i zip(x,y): if i!= j: prit("not equal!") break prit("equal!")
Άσκηση Γράψετε ένα πρόγραμμα που ελέγχει αν δύο λίστες είναι ίδιες χρησιμοποιώντας την for - else x = [1,2,3] y = [1,2,3,4] if le(x)!= le(y): prit("not equal!") for i,j i zip(x,y): if i!= j: prit("not equal!") break prit("equal!")
Σύνθεση λίστας (List comprehesio) με for/if Στα μαθηματικά ή στην καθομιλουμένη μπορούμε να ορίσουμε ένα σύνολο από στοιχεία περιγράφοντας τις ιδιότητες τους Π.χ.{x # : x {0,1,2,3,4,5,6,7,8,9}}: Τα τετράγωνα των αριθμών από το 0 μέχρι το 9 Η Pytho μας επιτρέπει να ορίσουμε λίστες με αυτόν τον τρόπο >>> [x*x for x i rage(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 54
Σύνθεση λίστας με for/if Μπορούμε να χρησιμοποιήσουμε συνδυασμό for και if Π.χ.{x # : x {0,1,2,3,4,5,6,7,8,9}, x%3 = 0}: Τα τετράγωνα των αριθμών από το 0 μέχρι το 9 που είναι πολλαπλάσια του 3 >>> [x*x for x i rage(10) if x % 3 == 0] [0, 9, 36, 81] 55
Σύνθεση λίστας με for/if {(x, y): x {0,1,2}, y {0,1,2}}: Ζεύγη με τους ακεραίους από 0 εως 2 >>> [(x, y) for x i rage(3) for y i rage(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] { x, y : x 0,1,2, y 0,1,2, x y} >>> [(x, y) for x i rage(3) for y i rage(3) if x <=y] [(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)] 56
Σύνθεση λίστας με for/if >>> girls = ['alice', 'berice', 'clarice'] >>> boys = ['chris', 'arold', 'bob'] Φτιάξε λίστα με strig με ζευγάρια από αγόρια και κορίτσια με το ίδιο αρχικό γράμμα ονόματος >>> [b+'+'+g for b i boys for g i girls if b[0] == g[0]] ['chris+clarice', 'arold+alice', 'bob+berice'] Φτιάξε λίστα με strig με ζευγάρια από αγόρια και κορίτσια στην ίδια θέση της λίστας >>> [b+'+'+g for b,g i zip(boys,girls)] ['chris+alice', 'arold+berice', 'bob+clarice'] 57
Η εντολή pass Χρησιμοποιείται αν δεν θέλουμε να κάνουμε τίποτα σε ένα block if ame == 'Ralph Auldus Melish': prit 'Welcome!' elif ame == 'Eid': # Not fiished yet... pass elif ame == 'Bill Gates': prit 'Access Deied' 58
Άσκηση Τι θα κάνουν τα παρακάτω προγράμματα; for i i rage(10): if i % 2 == 0: pass prit(i) for i i rage(10): pass prit('ok') 59
Η συνάρτηση eumerate Για κάθε στοιχείο μιας ακολουθίας (λίστας, πλειάδας, κλπ.), επιστρέφει το στοιχείο και ένα δείκτη στη θέση του. lst = [1,2,4,5,6,3,2] for idx,elem i eumerate(lst): prit(elem, at positio, idx) 1 at positio 0 2 at positio 1 4 at positio 2 5 at positio 3 6 at positio 4 3 at positio 5 2 at positio 6 60
Άσκηση Τύπωσε τα διακριτά στοιχεία μιας λίστας χρησιμοποιώντας την eumerate lst = [1,2,4,5,6,3,2,3,6,2,6,3,2] lst2 = [] for idx,elem i eumerate(lst): if elem ot i lst[0:idx]: lst2.apped(elem) prit(lst2) Αν δεν εµφανίζεται στα προηγούµενα [1, 2, 4, 5, 6, 3] 61
Σημαντικό! Εξασκηθείτε στο να «τρέχετε» ένα πρόγραμμα μόνοι σας και στο να παρακολουθείτε τις τιμές των μεταβλητών και τη ροή του προγράμματος Θα σας βοηθήσει και ο Pytho visualizer: http://pythotutor.com/visualize.html 62