Programming - Week 2
Programming - Week 2
Programming
Week 2 - Conditional Statements
36
Conditional Statements condition
!= NOT Equal to x != y
design as:
else:
print("It's a tie!")
○ just if
○ if and else >Home goals scored: 5
>Away goals scored: 0
○ if elif and else >The home team won!
number = int(input("Please type in a number: ")) number = int(input("Please type in a number: "))
39
Programming Task 12
● Please write a program which asks for the hourly wage, hours worked,
and the day of the week.
● The program should then print out the daily wages, which equal
hourly wage multiplied by hours worked, except on Sundays when the
hourly wage is doubled.
>Hourly wage: 94.5 liras
>Hours worked: 10
>Day of the week: Sunday
>Daily wages: 1890 liras
40
Programming Task 13
● Please write a program which asks the user for their age.
● The program should then print out a message based on whether the
user is of age or not, using 18 as the age of maturity.
● Bonus: If the user is 44 years old or older print this: “You are too old
for this sh*t”
>How old are you? 12
>You can’t play Dark Souls
41
Programming Task 14
● Please write a program which asks for two integer numbers.
● The program should then print out whichever is greater.
● If the numbers are equal, the program should print this: These are
equal
42
String Comparison
if "mahmut" > "can":
● comparison operators can also be print("Mahmut")
else:
used on strings print("Can")
43
Programming Task 15
● Please write a program which asks for two words.
● The program should then print out whichever is last.
● If the words are equal, the program should print this: These are same!
44
Combining Conditions
Truth Table
● you can combine conditions with
the help of logical operators First Second
AND OR
Input Input
● if both conditions are true
○ and operator returns true, False False false false
○ otherwise return false
False True false true
● if one of the conditions is true
○ or operator returns true True False false true
45
Combining Conditions
number = int(input("Please type in a number: "))
● range checking is a very common if number >= 5 and number <= 8:
print("The number is between 5 and 8")
thing
○ a >= x and x <= b
● We can simplify this in python: >Please type in a number: 6
>The number is between 5 and 8
○ a >= x <= b
46
Programming Task 16
● Please write a program that asks the user which community they belong to
● The program should then print out a message based on the list below:
○ New California Republic or NCR
○ Brotherhood of Steel
○ Caesar’s Legion
○ Great Kans
○ Vault Dweller
● If user is enters different input other than these community, print an error message
47
Programming Task 17
● Please write a program which asks for the amount of points received
and then prints out the grade attained according to the table.
Points Grade
>How many points [0-100]: 37
<0 you what? >Grade: fail
60-69 2
70-79 3
80-89 4
90-100 5
>100 impossible!
48
Programming Task 18
● Please write a program which asks the user for an integer number.
● If the number is divisible by
○ 3, the program should print out Fizz.
○ 5, the program should print out Buzz.
○ both 3 and 5, the program should print out FizzBuzz.
>Enter number: 9
>Fizz
>Enter number: 35
>FizzBuzz
49
Nested Conditions
● Conditional statements can also number = int(input("Please type in a number: "))
50
Programming Task 19
● Please convert this code to non-nested conditions
if number > 0:
if number % 2 == 0:
print("The number is even")
else:
print("The number is odd")
else:
print("The number is negative or zero")
51
Programming Task 20
● Please write a program which asks the user for a year, and then prints
out whether that year is a leap year or not.
● if the year is additionally divisible by 100, it is a leap year only if it also
divisible by 400.
52
End of the Week
Thanks Materials
mahmutcankovan@mu.edu.tr
● ?
● www.freecodecamp.com
● www.geeksforgeeks.com
Homework(s)
● null
53