Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Programming - Week 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming - Week 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Fundamentals of

Programming
Week 2 - Conditional Statements

30.09.2024 | DOT 1003 | Mahmut Can Kovan


Agenda
● Conditional Statements

36
Conditional Statements condition

● a block of code which is executed only if the


True False
condition in the statement is true.

Operator Description Example


other
block
== Equal to x == y block

!= NOT Equal to x != y

> Greater Than x > y


execution continues
>= Greater Than or equal to x >= y

< Less Than x < y

<= Less Than or equal to x <= y 37


Conditional Statements
goals_home = int(input("Home goals scored: "))
goals_away = int(input("Away goals scored: "))

if goals_home > goals_away:


print("The home team won!")
elif goals_away > goals_home:
● Conditional statements can be print("The away team won!")

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: "))

if number < 0: if number < 0:


print("The number is negative") print("The number is negative")
else:
print("The number is positive or zero")

>Please type in a number: 0 >Please type in a number: 0


>the number is positive or zero
38
Programming Task 11
● Please write a program which asks the user for a temperature in
degrees Fahrenheit, and then prints out the same in degrees Celsius.
● If the converted temperature falls below zero degrees Celsius, the
program should also print out "Brr! It's cold in here!".

>Please type in a temperature (F): 21


>21 degrees Fahrenheit equals -6.11111 degrees Celsius
>Brr! It's cold in here!

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

>How old are you? 18


>Here is Dark Souls. Lol

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

>Type the first number: 10


>Type the second one: 5
>First one is greater (10>5)

>Type the first number: 5


>Type the second one: 5
>These are equal (5=5)

42
String Comparison
if "mahmut" > "can":
● comparison operators can also be print("Mahmut")
else:
used on strings print("Can")

● String c is smaller than string m if


>Mahmut
it comes alphabetically before m
● Don’t Forget:
○ only standard english alphabet is
used
○ Compared characters must be same
case (lower or upper)

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!

>Type the first number: half-life


>Type the second one: skyrim
>skyrim comes alphabetically last

>Type the first number: fallout


>Type the second one: fallout
>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

True True true 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

number = int(input("Please type in a number: "))


if number >= 5 and number <= 8:
print("The number is between 5 and 8")

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

>Which community do you belong to?: NCR


>You’re belong to Fallout Universe.

>Which community do you belong to?: Bodosk


>Nope, you are not belong to Fallout Lore

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

0-49 fail How many points [0-100]: -3


Grade: you what?
50-59 1

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: "))

be nested within other if number > 0:


if number % 2 == 0:
conditional statements. print("The number is even")
else:
● We can achieve same result via print("The number is odd")
else:
using combined conditions print("The number is negative or zero")

>Please type in a number: 6


>The number is even

50
Programming Task 19
● Please convert this code to non-nested conditions

number = int(input("Please type in a number: "))

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.

>Please type in a year: 2011


>That year is not a leap year.

>Please type in a year: 1800


>That year is not a leap year.,

>Please type in a year: 2020


>That year is a leap year.

52
End of the Week
Thanks Materials
mahmutcankovan@mu.edu.tr
● ?
● www.freecodecamp.com
● www.geeksforgeeks.com

Homework(s)
● null

53

You might also like