ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό Έλεγχος Ροής Προγράµµατος Χειµερινό Εξάµηνο 2014
Ροή προγράµµατος n n Μέχρι τώρα έχουµε δει προγράµµατα απλής ροής Οι εντολές εκτελούνται η µία µετά την άλλη σύµφωνα µε την ακολουθία γραµµών του προγράµµατος Σε τυπικά προγράµµατα η ροή είναι πιο σύνθετη Χρήση συνθηκών για τον έλεγχο ροής Χρήση επαναλήψεων 2
Διαγράµµατα Ροής n n Τρόπος σχεδιασµού ενός αλγορίθµου Ο αλγόριθµος είναι το σχέδιο του προγράµµατος Βασικά µέρη ενός διαγράµµατος ροής temp > 30 no print(x) yes απόφαση εντολή ροή 3
Έλεγχος ροής µε if n if δυαδική έκφραση : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) if temp > 25: print('it is hot!') print('be sure to drink liquids.') οι εντολές στο µπλοκ είναι στοιχισµένες 4 θέσεις δεξιά temp = eval(input('enter the current temperature: ')) temp > 25 no yes print('it is hot!') print('be sure to drink liquids.') 4
Έλεγχος ροής µε if n if δυαδική έκφραση : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) if temp > 25: print('it is hot!') print('be sure to drink liquids.') print('goodbye!') µετά το µπλόκ, εκτελείται ανεξάρτητα από την έκβαση της if temp = eval(input('enter the current temperature: ')) temp > 25 no yes print('it is hot!') print('be sure to drink liquids.') print('goodbye!') 5
Έλεγχος ροής µε if/else n if δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) if temp > 25: print('it is hot!') print('be sure to drink liquids.') print('it is not hot.') print('bring a jacket.') print('goodbye!') 6
Έλεγχος ροής µε if/else n if δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) no print('it is not hot.') print('bring a jacket.') temp > 25 yes print('it is hot!') print('be sure to drink liquids.') print('goodbye!') 7
Έλεγχος ροής µε if/elif/else n if δυαδική έκφραση : µπλοκ εντολών elif δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών x = eval(input('enter a number: ')) if x > 0: print('the number is positive') elif x < 0: print('the number is negative') print('the number is zero') 8
Έλεγχος ροής µε if/elif/else n if δυαδική έκφραση : µπλοκ εντολών elif δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών µπορούµε να βάλουµε όσα διαδοχικά elif θέλουµε! Το elif σηµαίνει else if x = eval(input('enter a number: ')) yes print('the number is negative') x > 0 no x < 0 no yes print('the number is positive') print('the number is zero') 9
Φωλιασµένα if x = eval(input('enter a number: ')) if x > 0: if x > 1000: print('a large positive number') else : print('the number is positive') elif x < 0: print('the number is negative') print('the number is zero') x = eval(input('enter a number: ')) yes print('the number is negative') x > 0 no x < 0 no yes yes x > 1000 no print('a large positive number') print('the number is positive') print('the number 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
Τελεστές σύγκρισης n x == y το x ισούται µε το y. n x < y το x είναι µικρότερο από το y. n x > y το x είναι µεγαλύτερο από το y. n x >= y το x είναι µεγαλύτερο ή ίσο από το y. n x <= y το x είναι µικρότερο ή ίσο από το y. n x!= y το x είναι διάφορο του y. n x is y x και y είναι το ίδιο αντικείµενο. n x is not y x και y είναι διαφορετικά αντικείµενα. n x in y το x είναι µέλος της ακολουθίας y. n x not in y το x δεν είναι µέλος της ακολουθίας y. 12
Λεξικογραφική σειρά >>> "alpha" < "beta" True >>> [1, 2] < [2, 1] True >>> [2, [1, 4]] < [2, [1, 5]] True 13
Παραδείγµατα name = input('what is your name? ') if 's' in name: print('your name contains the letter "s".') print('your name does not contain the letter "s".') 14
Παραδείγµατα number = int(input('enter a number between 1 and 10: ')) if number <= 10: if number >= 1: print('great!') print('wrong!') print('wrong!') number = int(input('enter a number between 1 and 10: ')) if 1<=number<=10: print('great!') print('wrong!') 15
Παραδείγµατα (and, or, not) number = int(input('enter a number between 1 and 10: ')) if number>=1 and number<=10: print('great!') print('wrong!') number = int(input('enter a number between 1 and 10: ')) if number<1 or number>10: print('wrong!') print('great!') number = int(input('enter a number between 1 and 10: ')) if number>=1 and not number>10: print('great!') print('wrong!') 16
Εναλλακτικοί τρόποι χρήσης if >>> name = input('please enter your name: ') or '<unknown>' Please enter your name: >>> name '<unknown>' >>> name = input('please enter your name: ') or '<unknown>' Please enter your name: Joe >>> name 'Joe' επιστρέφει το or µόνο αν το input() είναι κενό (False) False είναι τα ακόλουθα: 0,'', (), {}, [], None >>> y=0 >>> x = 5 if y>1 else 0 >>> x 0 το ίδιο µε: if y>1: x=5 x=0 17
Άσκηση n Τί πρόβληµα έχει ο παρακάτω κώδικας; n temp = int(input('please input the current temperature: ')) if temp > 5: print('it is cool') elif temp > 25: print('it is hot!') print('it is freezing!') Πώς θα φτιάξουµε το πρόγραµµά µας; δεν θα εκτελεστεί ποτε! πρέπει να προσέχουµε τη σειρά µε την οποία βάζουµε τις συνθήκες!... if temp > 25: print('it is hot!') elif temp > 5: print('it is cool')... 18
Άσκηση n Είσοδος: ένας αριθµός από το 1 ως το 4 n Έξοδος: τύπωσε John αν είναι 1, Paul αν είναι 2, Maria αν είναι 3, Sue αν είναι 4 c = int(input('please input a number between 1 and 4: ')) if c == 1: print('john') elif c == 2: print('paul') elif c == 3: print('maria') elif c == 4: print('sue') 19
Άσκηση n Είσοδος: ένας αριθµός από το 1 ως το 4 n Έξοδος: τύπωσε John αν είναι 1, Paul αν είναι 2, Maria αν είναι 3, Sue αν είναι 4 n Χωρίς να χρησιµοποιήσετε if! d = {1:'John',2:'Paul',3:'Maria',4:'Sue'} c = int(input('please input a number between 1 and 4: ')) print(d[c]) 20
Επαναλήψεις (loops) n Στα προγράµµατά µας µπορεί να θέλουµε να τρέξουµε την ίδια σειρά εντολών πολλές φορές µε πιθανές παραµετροποιηµένες µικροαλλαγές σε κάθε επανάληψη είτε γνωρίζοντας πόσες φορές (for) είτε ενόσω µια συνθήκη είναι αληθής (while) no x = 1 x <= 100 yes print(x) x = x + 1 21
Ο βρόγχος while n Eνόσω η συνθήκη είναι αληθής εκτελείται το µπλοκ εντολών που είναι στοιχισµένες κάτω της x =1 while x <= 100: print(x) x += 1 κενό αλφαριθµητικό name = '' while not name: name = input('please enter your name: ') print('hello, %s!' % name) True, αν το name είναι κενό Αν θέλουµε το διάστηµα ' ' να µην είναι αποδεκτό σαν όνοµα, βάζουµε: while not name or name.isspace() ή while not name.strip() 22
Ο βρόγχος for n Χρησιµοποιείται αν θέλουµε να επαναλάβουµε ένα µπλοκ κώδικα, για κάθε τιµή ενός συνόλου numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for x in numbers: print(x) words = ['this', 'is', 'an', 'ex', 'parrot'] for x in words: print(x[-1]) for x in range(1,101): print(x) ορίζει ένα διάστηµα τιµών, όπως και στο slicing 23
Άσκηση n Γράψτε ένα πρόγραµµα που θα παίρνει ένα αλφαριθµητικό από το χρήστη και θα τυπώνει τα φωνήεντα του s = input('please input a string: ') for c in s: if c in 'aeoiu': print(c) 24
Άσκηση n Τύπωσε τους ζυγούς αριθµούς µέχρι το 10 for x in range(11): if x % 2 == 0: print(x) range(11)=range(0,11)= [0,1,2,3,4,5,6,7,8,9,10] for x in range(0,11,2): print(x) 25
Άσκηση n Δίνεται µια λίστα lst αριθµών. Βρές το γινόµενό τους lst = [3,4,6,5] prod = 1 for x in lst: prod *= x print(prod) το ίδιο µε prod = prod*x 26
Επαναλήψεις σε λεξικά n d = {'x': 1, 'y': 2, 'z': 3} for key in d: print(key, 'corresponds to', d[key]) Εναλλακτικά: d = {'x': 1, 'y': 2, 'z': 3} for key, value in d.items(): print(key, 'corresponds to', value) z corresponds to 3 y corresponds to 2 x corresponds to 1 Υπενθύµηση: στα λεξικά δεν υπάρχει συγκεκριµένη σειρά προσπέλασης των ζευγαριών 27
Παράλληλες επαναλήψεις names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] for i in range(len(names)): print (names[i], 'is', ages[i], 'years old') range(4)=range(0,4) anne is 12 years old beth is 45 years old george is 32 years old damon is 102 years old 28
Παράλληλες επαναλήψεις - zip names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] for name, age in zip(names, ages): print(name, 'is', age, 'years old') anne is 12 years old beth is 45 years old george is 32 years old damon is 102 years old names = ['anne', 'beth', 'george', 'damon', 'paul', 'sue'] ages = [12, 45, 32, 102] for name, age in zip(names, ages): print(name, 'is', age, 'years old') Τι θα τυπώσει; Το ίδιο! Η zip σταµατά στην κοντύτερη σειρά 29
Παράλληλες επαναλήψεις - zip names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] lst = (3,45,66) d = {'1':3, '2':4} for n,a,l,di in zip(names, ages, lst, d): print(n,a,l,d[di]) anne 12 3 4 beth 45 45 3 30
Διακοπή βρόγχου µε break n Χρησιµοποιούµε break όταν θέλουµε να σταµατήσουµε ένα loop n Π.χ. βρες το µεγαλύτερο ακέραιο τετράγωνο < 100 from math import sqrt for n in range(99, 0, -1): root = sqrt(n) if root == int(root): print (n) break κατέβασµα από το 99 στο 1 µε αρνητικό βήµα (-1) Π.χ. προσπαθήστε: list(range(0,100,5)) είναι το root ακέραιος; 31
While True µε break n Όταν θέλουµε να βγούµε από το βρόγχο ακριβώς µόλις ικανοποιηθεί µια συνθήκη εξόδου, χωρίς να εκτελέσουµε τις υπόλοιπες εντολές του βρόγχου πάντα αληθές while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!" µόνη πιθανή έξοδος 32
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!" 33
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! 34
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! x == 40 35
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! x == 40 36
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! x == 40 37
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! x == 40 38
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! x == 60 39
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! x == 60 40
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! x == 60 41
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! How many states are in the U.S.? 50! x == 60 42
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! How many states are in the U.S.? 50! x == 50 43
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! How many states are in the U.S.? 50! Correct! There are 50 states in the U.S.! x == 50 44
While True µε break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! How many states are in the U.S.? 60! The answer is NOT correct!! How many states are in the U.S.? 50! Correct! There are 50 states in the U.S.! x == 50 45
Άσκηση n Ποια η διαφορά µεταξύ των 2 προγραµµάτων; while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!" while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!" 46
Άσκηση n Γράψε το ίδιο πρόγραµµα χωρίς χρήση break while True: x = int(input("how many states are in the U.S.? ")) if x == 50: print("correct! There are 50 states in the U.S.") break; print("the answer is NOT correct!" x = 0 while x!=50 : x = int(input("how many states are in the U.S.? ")) if x!=50 : print("the answer is NOT correct!") print("correct! There are 50 states in the U.S.") 47
Άσκηση n Δίνεται µια λίστα lst. Γράψε ένα πρόγραµµα που να ελέγχει αν η λίστα είναι ταξινοµηµένη lst = [3,4,6,5,7] is_sorted = True for i in range(0,len(lst)-1): if lst[i] > lst[i+1]: print('list',lst, 'is not sorted!') is_sorted = False break if is_sorted: print('list', lst, 'is sorted!') βοηθητική µεταβλητή έλεγχος αν υπαρχει στοιχείο µεγαλύτερο από το επόµενο 48
H break σπάει τον εσωτερικότερο βρόγχο n Τι θα τυπώσει το παρακάτω πρόγραµµα; for i in range(3): for j in range(3): print(i,j) if j==1: break 0 0! 0 1! 1 0! 1 1! 2 0! 2 1! Δες το στο http://pythontutor.com/visualize.html 49
Η εντολή continue n n Τερµατίζει την τρέχουσα επανάληψη και πάει στην επόµενη Παράδειγµα: τύπωσε όλους τους χαρακτήρες ενός αλφαριθµητικού, εκτός από τα φωνήεντα s = input('please input a string: ') for c in s: if c in 'aeoiu': continue print(c) Πήγαινε κατευθείαν στην επόµενη επανάληψη 50
Χρήση else στη for n Εκτελείται µόνο όταν το for-loop δεν έχει «σπάσει» λόγω break from math import sqrt for n in range(99, 90, -1): root = sqrt(n) if root == int(root): print (n) break print("didn't find it") Εκτελείται αν το for τερµατίσει χωρίς break 51
Σύνθεση λίστας µε for/if >>> [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> [x*x for x in range(10) if x % 3 == 0] [0, 9, 36, 81] >>> [(x, y) for x in range(3) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] >>> girls = ['alice', 'bernice', 'clarice'] >>> boys = ['chris', 'arnold', 'bob'] >>> [b+'+'+g for b in boys for g in girls if b[0] == g[0]] ['chris+clarice', 'arnold+alice', 'bob+bernice'] 52
Η εντολή pass n Χρησιµοποιείται αν δεν θέλουµε να κάνουµε τίποτα σε ένα block if name == 'Ralph Auldus Melish': print 'Welcome!' elif name == 'Enid': # Not finished yet... pass elif name == 'Bill Gates': print 'Access Denied' 53
Άσκηση n Τι θα κάνουν τα παρακάτω προγράµµατα; for i in range(10): if i % 2 == 0: pass print(i) for i in range(10): pass print('ok') 54
Η συνάρτηση enumerate n n Για κάθε στοιχείο µιας ακολουθίας (λίστας, πλειάδας, κλπ.), επιστρέφει το στοιχείο και ένα δείκτη στη θέση του. Π.χ. τύπωσε τα διακριτά στοιχεία µιας λίστας lst = [1,2,4,5,6,3,2,3,6,2,6,3,2] lst2 = [] for idx,elem in enumerate(lst): if elem not in lst[0:idx]: lst2.append(elem) print(lst2) Αν δεν εµφανίζεται στα προηγούµενα [1, 2, 4, 5, 6, 3] 55
Σηµαντικό! n n Εξασκηθείτε στο να «τρέχετε» ένα πρόγραµµα µόνοι σας και στο να παρακολουθείτε τις τιµές των µεταβλητών και τη ροή του προγράµµατος Θα σας βοηθήσει και ο Python visualizer: http://pythontutor.com/visualize.html 56