You are on page 1of 7

ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ

Γ γυμναςίου

ΔΟΜΕ΢ ΕΠΑΝΑΛΗΨΗ΢

 Διαφάνειες 60 - 73 https://mathisis.org/cms/books/python.pdf

ΔΟΜΗ ΕΠΑΝΑΛΗΨΗ΢ while…

i=0
while i < 100:
# do_something()
i += 1 # i = i+1

 1θ Δραςτθριότθτα
Να γραφεί πρόγραμμα που να εμφανίηει 10 φορζσ «Welcome to Python!”

 2θ Δραςτθριότθτα

Using a counter variable to exit a loop


Εκτελζςτε τον παρακάτω κϊδικα και παρατθρείςτε τί εμφανίηει.

counter = 5
while counter != 0: # while counter:
print("Inside the loop.", counter)
counter -= 1
print("Outside the loop.", counter)

 3θ Δραςτθριότθτα

Γράψτε μία επανάλθψθ while θ οποία κα τυπϊνει τουσ ακεραίουσ από το 0 ζωσ ζναν κετικό
ακζραιο n ο οποίοσ κα ειςάγεται από τον χριςτθ.

 4θ Δραςτθριότθτα

Δθμιουργιςτε επανάλθψθ while που κα μετράει από το 1 μζχρι και το 10 και κα εμφανίηει μόνο τουσ
άρτιουσ αρικμοφσ.

 5θ Δραςτθριότθτα

Εκτελζςτε τον παρακάτω κϊδικα και παρατθρείςτε τί εμφανίηει.

n=3
while n > 0:
print(n + 1)

1
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

n -= 1
else:
print(n)

 6θ Δραςτθριότθτα

Scenario
Listen to this story: a boy and his father, a computer programmer, are playing with wooden blocks. They
are building a pyramid.

Their pyramid is a bit weird, as it is actually a pyramid-shaped wall - it's flat. The pyramid is stacked
according to one simple principle: each lower layer contains one block more than the layer above.

The figure illustrates the rule used by the builders:

Your task is to write a program which reads the number of blocks the builders have, and outputs the
height of the pyramid that can be built using these blocks.

Note: the height is measured by the number of fully completed layers - if the builders don't have a
sufficient number of blocks and cannot complete the next layer, they finish their work immediately.

Test your code using the data we've provided.

Test Data

Sample input: 6 Sample input: 20

Expected output: The height of the pyramid: 3 Expected output: The height of the pyramid: 5
Sample input: 1000 Sample input: 2

Expected output: The height of the pyramid: 44 Expected output: The height of the pyramid: 1

blocks = int(input("Enter the number of blocks: "))


# Write your code here.

2
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

 7θ Δραςτθριότθτα

Ρίχνουμε ηάρι τυχαία μζχρι να φζρουμε 6


H ςυνάρτθςθ random.randint(m,n) επιςτρζφει ζνα ακζραιο τυχαία μεταξφ m και n

import random
zari = 0
while (zari != 6):
zari = random.randint(1,6)
print(zari)
print("Εξάρι!")

Να γράψετε ζνα πρόγραμμα με βρόγχο επανάλθψθσ while όπου:


1. Θα "ρίχνει" και κα εμφανίηει δφο ηάρια τυχαία. Για παράδειγμα 1, 4.

2. Οι ηαριζσ κα επαναλαμβάνονται μζχρι να ζρκουν εξάρεσ, δθλαδι και τα δυο ηάρια κα είναι 6.

3. ΢το τζλοσ το πρόγραμμα κα εμφανίηει το πλικοσ των ηαριϊν που χρειάςτθκαν μζχρι να
ζρκουν οι εξάρεσ.

΢υμβουλζσ
 Δείτε το παράδειγμα 2 για να δείτε πωσ
παράγουμε τυχαίουσ αρικμοφσ για το ηάρι.
 Θα χρειαςτείτε δυο ηάρια, άρα δφο μεταβλθτζσ
(π.χ. zari1 και zari2)
 Θα χρειαςτείτε επίςθσ και μια μεταβλθτι για να
μετράτε τισ ηαριζσ
 Εξάρεσ ςθμαίνει το άκροιςμα των δυο ηαριϊν
είναι 12. Άρα επαναλαμβάνουμε όςο το
άκροιςμα των ηαριών δεν είναι 12.

 8θ Δραςτθριότθτα

A junior magician has picked a secret number. He has hidden it in a variable named secret_number. He
wants everyone who run his program to play the Guess the secret number game, and guess what
number he has picked for them. Those who don't guess the number will be stuck in an endless loop
forever! Unfortunately, he does not know how to complete the code.

Your task is to help the magician complete the code in the editor in such a way so that the code:

 will ask the user to enter an integer number;


 will use a while loop;
 will check whether the number entered by the user is the same as the number picked by the
magician. If the number chosen by the user is different than the magician's secret number, the
user should see the message "Ha ha! You're stuck in my loop!" and be prompted to enter a

3
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

number again. If the number entered by the user matches the number picked by the magician,
the number should be printed to the screen, and the magician should say the following
words: "Well done, muggle! You are free now."

The magician is counting on you! Don't disappoint him.

secret_number = 777

print(
"""
+================================+
| Welcome to my game, muggle! |
| Enter an integer number |
| and guess what number I've |
| picked for you. |
| So, what is the secret number? |
+================================+
""")
x= int(input("Enter secret number: "))
# Write your code here.

 9θ Δραςτθριότθτα
Να γράψετε ζνα πρόγραμμα ςτο οποίο να προςκζτει 3 αρικμοφσ. Θα πρζπει να ςασ ηθτά 3
διαφορετικοφσ αρικμοφσ και να επαναλαμβάνει τισ πράξεισ, μζχρι να πατιςετε κάποιο κουμπί εξόδου
(π.χ. το “y”).

4
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

ΠΑΡΑΔΕΙΓΜΑΣΑ ΓΙΑ ΜΕΛΕΣΗ

We're going to show you how to use this newly learned loop to find the largest number from a large
set of entered data.

# Store the current largest number here.


largest_number = -999999999
# Input the first value.
number = int(input("Enter a number or type -1 to stop: "))
# If the number is not equal to -1, continue.
while number != -1:
# Is number larger than largest_number?
if number > largest_number:
# Yes, update largest_number.
largest_number = number
# Input the next number.
number = int(input("Enter a number or type -1 to stop: "))
# Print the largest number.
print("The largest number is:", largest_number)

A program that reads a sequence of numbers and counts how many numbers are even and how many
are odd. The program terminates when zero is entered.

odd_numbers = 0
even_numbers = 0

# Read the first number.


number = int(input("Enter a number or type 0 to stop: "))

# 0 terminates execution.
while number != 0: # while number:
# Check if the number is odd.
if number % 2 == 1: # if number % 2:
# Increase the odd_numbers counter.
odd_numbers += 1
else:
# Increase the even_numbers counter.
even_numbers += 1
# Read the next number.
number = int(input("Enter a number or type 0 to stop: "))

# Print results.
print("Odd numbers count:", odd_numbers)
print("Even numbers count:", even_numbers)

5
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

Επαλικευςθ κωδικοφ ώςτε να ζχει τουλάχιςτον 6 χαρακτιρεσ

password = input("Δϊςε νζο κωδικό, 6 χαρακτιρεσ τουλάχιςτον") #password = ""


while len(password) < 6:
password = input("Δϊςε νζο κωδικό, 6 χαρακτιρεσ τουλάχιςτον")
print("Ο κωδικόσ ςασ άλλαξε!")

ΔΟΜΗ ΕΠΑΝΑΛΗΨΗ΢ for…

for counter in range(3): Ασ εξετάςουμε τισ εντολζσ: Σο counter είναι μια μεταβλθτι. Με τθ
print(counter) ςυνάρτθςθ range(3), θ μεταβλθτι παίρνει τιμζσ από το 0 (που
είναι θ αρχικι τιμι) μζχρι και το 2. O αρικμόσ 3 μασ λζει, δθλαδι,
να πάρουμε 3 αρικμοφσ με τθ ςειρά, ΑΛΛΑ ο πρώτοσ είναι το 0
(άρα οι αρικμοί είναι 0, 1 και 2).

for counter in range(1, 13): ΢τθ γραμμι πιο πάνω, κζτουμε το εφροσ να είναι από το 1 μζχρι το
print(counter) 13. Αν βάηαμε range(13), τότε κα ξεκινοφςε από το 0 μζχρι το 12.
Εμείσ κζλουμε να ξεκινά από το 1 μζχρι το 12.

i=0
while i < 100: for i in range(100):
# do_something() # do_something()
i += 1

 10θ Δραςτθριότθτα

Εκτελζςτε τον παρακάτω κϊδικα και παρατθρείςτε τί εμφανίηει.

for i in range(10):
print("The value of i is currently", i)

for i in range(2, 8):


print("The value of i is currently", i)

for i in range(1, 1):


print("The value of i is currently", i)

for i in range(2, 1):


print("The value of i is currently", i)

# the range()'s second argument must be greater than the first.

6
ΠΡΟΣΤΠΟ ΓΤΜΝΑ΢ΙΟ ΙΩΝΙΔΕΙΟΤ ΢ΧΟΛΗ΢ ΠΕΙΡΑΙΑ
Γ γυμναςίου

for i in range(2, 8, 3): The first argument passed to the range() function tells us
print("The value of i is currently", i) what the starting number of the sequence is (hence 2 in the
output). The second argument tells the function where
to stop the sequence (the function generates numbers up to
the number indicated by the second argument, but does not
include it). Finally, the third argument indicates the step,
which actually means the difference between each number
in the sequence of numbers generated by the function.

2 (starting number) → 5 (2 increment by 3 equals 5 - the


number is within the range from 2 to 8) → 8 (5 increment
by 3 equals 8 - the number is not within the range from 2
to 8, because the stop parameter is not included in the
sequence of numbers generated by the function.)

 11θ Δραςτθριότθτα

Create a for loop that counts from 0 to 10, and prints odd numbers to the screen.

 12θ Δραςτθριότθτα

Εκτελζςτε τον παρακάτω κϊδικα και παρατθρείςτε τί εμφανίηει.

n = range(4)
for num in n:
print(num - 1)
else:
print(num)

 13θ Δραςτθριότθτα

Εκτελζςτε τον παρακάτω κϊδικα και παρατθρείςτε τί εμφανίηει.

for i in range(0, 6, 3):


print(i)

You might also like