Programming - Week 3 Part1
Programming - Week 3 Part1
Programming
Week 3 - Functions, Introduction
to Loops and Lists
55
What is Function?
● a block of code which is executed when called
● int(), float(), print(), input() is an example of built-in,
functions which were used in this course
● We can create our own functions
● We can call functions as much as we want
56
Method vs. Function. What is the difference?
● Method is almost same thing as function
● if we define a function in a class it is called a method?
● Methods are attached to the object they are called on
● Syntax is slightly different
57
How to Create a Function
def my_message():
print("No gods or kings. Only man.")
my_message()
58
Programming Task 21
● Please create a function which takes one argument as a name, no
return parameter and print this argument like below using f-string
59
Programming Task 22
● Please create a function which takes one argument as a name, has
return parameter and print this argument like below using f-string
60
Programming Task 23
● Please create a function named sum which takes two argument and
return of their sum
#TODO
#main code
print(sum(3,2))
61
Programming Task 24
● Find the problem and fix it.
greeting("Andrew Ryan")
greeting("Gordon Freeman")
62
Programming Task 25
● Please create a function which takes three arguments as an integer
and print out the arithmetic mean of them
#TODO
#main code
mean(3,2,1)
63
What is Loop?
● a block of code which is iterate finitely or indefinitely
● We call loops as one of fundamental control structures
● The code block inside loop would iterate (repeat) as much as we want
● Loops are easy to learn but hard to master structures
● Quality of your codes in terms of “time complexity” is depend on your
loops (most of the time)
64
While Loop
print("Please type in a number as much as you want")
print("Type -1 to quit")
my_flag = True
while my_flag:
● Please don’t use break and my_input = input("Input: ")
continue!
print(f"Your input is: {my_input}")
65
Programming Task 26
● Please create program which ask name and if the name is correct, It
will print “You're goddamn right.” and finish.
66
Programming Task 27
● Please create program which asks the user for a password. After that,
program ask password again to verification. If correct, finish.
Otherwise ask again and again.
67
Programming Task 28
● Please create a countdown which ask user for start number and
countdown to zero.
● Print every step. When counter reach zero, print Kaboom and exit.
68
Programming Task 29
● Please create program which ask the user password for 3 times
● If correct password entered, print “Welcome” and finish. Otherwise
print “Try Again” and ask password again
● If user couldn’t enter correct password after 3 trial, print “Incorrect
Password. Exterminate...” and finish.
>Password: 123456
>Try Again
>Password: 654321
>Try Again
>Password: 314159
>Welcome
69
Programming Task 30 - Assignment 1
● Please create program which ask integers. Whenever user enters 0,
program finish and show the results
○ Count how many integers does user enter and print
○ Calculate the sum of these numbers and print
○ Calculate arithmetic mean of these numbers and print
○ Calculate how many inputs are odd and even seperately and print