02. Conditionals
02. Conditionals
Conditionals
Exercise 1 β Even or odd
Ask the user to enter an integer. Print whether the number is even or odd.
Example:
Enter a number: 17
17 is oddExercise 2 β Password check
Create a variable containing a password.
Ask the user to enter a password. If it is correct, print:
Access grantedOtherwise print:
Access deniedExercise 3 β Temperature warning
Ask the user for a temperature in degrees Celsius.
- Below 0 β print
Freezing - From 0 to 20 β print
Cold - From 21 to 30 β print
Comfortable - Above 30 β print
Hot
Use if, elif and else.
Print the temperature using an f-string.
Exercise 4 β Age category
Ask the user for their age.
Print:
- under 12 β
Child - 12β17 β
Teenager - 18β64 β
Adult - 65 or older β
Senior
Then print a second message containing the age and category, for example:
Age: 23 - Category: AdultExercise 5 β Simple calculator
Ask the user for two numbers and an operator (+, -, * or /).
Calculate and print the result.
For example:
First number: 12
Second number: 4
Operator: /
12 / 4 = 3.0If the user enters an operator that isn't supported, print:
Unknown operatorExercise 6 β Ternary operator
Ask the user for their age.
Use a ternary operator to create a variable called status:
- 18 or older β
"adult" - otherwise β
"minor"
Then print:
You are an adult.or
You are a minor.Exercise 7 β Shopping discount
Ask the user for the price of a product.
If the price is:
- less than β¬50 β no discount
- β¬50 or more β 10% discount
Calculate the final price and print something like:
Original price: β¬80.00
Discount: 10%
Final price: β¬72.00Use a ternary operator to determine the discount percentage.
Exercise 8 β Exam result
Ask the user for a student's score as a percentage.
Print:
- below 50 β
Failed - 50β59 β
Passed - 60β69 β
Good - 70β79 β
Very good - 80β100 β
Excellent
Then print the result together with the score.
For example:
Score: 73%
Result: Very goodAssume the user enters a valid score between 0 and 100.
Exercise 9 β Train ticket
Ask the user for:
- their name
- their age
- whether they want a first-class ticket (
1or2)
The basic ticket price is β¬20.
Calculate the price:
- under 12 years β β¬10
- 12β64 years β β¬20
- 65 or older β β¬15
A first-class ticket costs β¬10 extra.
Print a small ticket:
====================
Train ticket
Passenger: Alice
Age: 67
Class: 1
Price: β¬25
====================Use input(), casts, if/elif/else, variables and f-strings.
Exercise 10 β Mini login system
Create variables containing:
username = "admin"
password = "python123"Ask the user for their username and password.
The program should:
Check whether the username is correct.
If it is correct, check whether the password is correct.
Print an appropriate message:
- both correct β
Welcome, admin! - username correct but password wrong β
Incorrect password - username wrong β
Unknown user
- both correct β
Afterwards, use a ternary operator to create a variable called message containing either "Login successful" or "Login failed", and print it.
Important: Do this without using and, or, or not. The nested if is intentional here.
Not, and, or
Exercise 1 β Weekend
Ask the user to enter a day of the week.
Print:
"It's the weekend!"if the day is"Saturday"or"Sunday"."It's a weekday."otherwise.
Make sure the comparison is done with ==.
Exercise 2 β Access to a game
Ask the user for their age and whether they have a parental permission ("yes" or "no").
A user can play the game if they are 18 or older, or if they have parental permission.
Print either:
You can play the game.or:
You cannot play the game.Use or.
Exercise 3 β School discount
A shop gives a student discount if the customer:
- is younger than 26 and
- is a student.
Ask for the age and whether the person is a student ("yes" or "no").
Print the price after a 20% discount if they qualify.
For example:
Age: 21
Student: yes
Price: 50
Discount: 20%
Final price: 40.0Exercise 4 β Login attempt
Create these variables:
correct_username = "admin"
correct_password = "python123"Ask the user for a username and password.
The login is successful only if both are correct.
Use and and ==.
Print:
Login successfulor:
Login failedExercise 5 β NOT allowed
Ask the user whether they are "allowed" to enter a room.
If the answer is not "yes", print:
You are not allowed to enter.Otherwise print:
Welcome!Use not explicitly in the condition.
Exercise 6 β Free shipping
Ask the user for:
- the total price of their order
- whether they are a member (
"yes"or"no")
Free shipping applies if:
- the order is β¬50 or more or
- the customer is a member.
Print the shipping cost:
Shipping: β¬0or:
Shipping: β¬5Then print the total including shipping.
Exercise 7 β Understanding order of operations
Ask the user for three numbers: a, b and c.
Calculate:
result = a + b * cPrint the result.
Then calculate:
result = (a + b) * cPrint that result too.
For example:
a: 2
b: 3
c: 4
Without parentheses: 14
With parentheses: 20Question: Why are the two results different?
Exercise 8 β Valid temperature
Ask the user for a temperature.
A temperature is considered normal when it is between 18 and 25 degrees, inclusive.
Use a condition containing both and and comparisons.
Print:
Normal temperatureor:
Temperature is outside the normal rangeThen use a ternary operator to create a variable called message containing "OK" or "Check temperature" and print it.
Exercise 9 β Cinema ticket
Ask the user for:
- their age
- whether they have a student card (
"yes"or"no") - whether it is Tuesday (
"yes"or"no")
The normal ticket price is β¬12.
The customer gets a β¬4 discount if they are:
- younger than 18 or
- a student.
On Tuesdays, everyone gets another β¬2 discount.
Calculate and print the final price.
For example:
Age: 20
Student: yes
Tuesday: yes
Original price: β¬12
Discount: β¬6
Final price: β¬6Important: Think carefully about how you combine and and or.
Exercise 10 β Can I go outside?
Ask the user:
- Is it raining? (
yes/no) - Is it cold? (
yes/no) - Do you have an umbrella? (
yes/no)
The person can go outside if:
- it is not raining, or
- it is raining but they have an umbrella.
However, if it is cold, they should print:
Take a jacket!otherwise:
No jacket needed.Finally, use a ternary operator to print either:
Have fun outside!or:
Better stay inside.More exercises
Exercise 1 β Student results
Write a program that asks for:
- student's name
- age
- score for programming
- score for networking
- score for databases
- number of missed classes
Calculate the average score.
The student passes if:
- the average is at least 50 and
- none of the individual scores is below 40.
Print the student's name, the three scores and the average.
Then print a result:
- average β₯ 80 β
Excellent - average β₯ 70 β
Very good - average β₯ 60 β
Good - average β₯ 50 β
Passed - otherwise β
Failed
If the student has more than 10 missed classes, also print:
Warning: many classes missedFinally, use a ternary operator to print whether the student is "Ready for the next year" or "Needs to improve".
Exercise 2 β Computer purchase
A computer shop asks a customer for:
- customer name
- age
- budget
- amount of RAM wanted in GB
- amount of storage wanted in GB
- whether they want a laptop (
yes/no) - whether they are a student (
yes/no)
Calculate a computer price using these rules:
Base price: β¬500
- 16 GB RAM β +β¬100
- more than 16 GB RAM β +β¬200
- 512 GB storage β +β¬50
- more than 512 GB β +β¬150
- laptop β +β¬200
Students receive a β¬100 discount if the computer costs at least β¬700.
Then determine whether the customer can afford it.
Print something like:
==============================
Computer purchase
Customer: Alice
RAM: 16 GB
Storage: 1000 GB
Laptop: yes
Student: yes
------------------------------
Price: β¬850
Discount: β¬100
Final price: β¬750
Budget: β¬800
Can afford: yes
==============================If the customer cannot afford it, print an appropriate message.
Exercise 3 β Hotel booking
Create a program that asks for:
- guest name
- age
- number of nights
- number of adults
- number of children
- room type (
single,double,family) - whether the guest is a member (
yes/no)
Use these prices:
- single room β β¬60 per night
- double room β β¬90 per night
- family room β β¬130 per night
Children cost β¬15 per night each.
Calculate the total price.
Apply these rules:
- members get 10% discount
- stays of 7 nights or more get another 10% discount
- guests younger than 18 cannot book a room themselves
Print the complete booking information and final price.
If the room type is invalid, print:
Unknown room typeIf the guest is under 18, print:
You must be at least 18 to make a booking.Otherwise, calculate the price.
Challenge: Make sure the discounts are calculated in the correct order.
Exercise 4 β Simple bank account
Write a program that simulates a very small bank account.
Start with:
balance = 1000Ask the user for:
- name
- age
- amount they want to deposit
- amount they want to withdraw
- whether they have a debit card (
yes/no)
Perform the deposit first.
Then process the withdrawal.
A withdrawal is allowed only if:
- the amount is not greater than the balance
- the amount is greater than 0
- the customer is at least 18
If the customer tries to withdraw more money than available:
Insufficient fundsIf they are under 18:
Withdrawal not allowedIf the withdrawal is successful, subtract it from the balance.
Finally, print:
====================
Bank account
Name: Alice
Deposit: β¬200
Withdrawal: β¬350
Balance: β¬850
Debit card: yes
====================If the final balance is below β¬100, print:
Warning: low balanceUse and, or, not, ==, casts, if, elif, else, and at least one ternary operator.
Exercise 5 β University admission
Write a program that determines whether a student can enter a particular study program.
Ask for:
- student's name
- age
- mathematics score
- programming score
- networking score
- English score
- number of failed courses
- whether they already have a diploma (
yes/no)
Calculate the average score.
The student gets one of these results:
Excellent candidate
If:
- average β₯ 75
- mathematics β₯ 60
- programming β₯ 60
- failed courses == 0
Good candidate
If:
- average β₯ 60
- mathematics β₯ 50
- programming β₯ 50
- failed courses β€ 1
Possible candidate
If:
- average β₯ 50
- programming β₯ 50
- they have a diploma
Not admitted
Otherwise.
There is one additional rule:
If the student is younger than 18, they cannot be admitted regardless of their scores.
Print a report such as:
================================
University admission
Name: Thomas
Age: 19
Mathematics: 72
Programming: 81
Networking: 68
English: 75
Average: 74.0
Failed courses: 0
Diploma: yes
Result: Good candidate
================================Finally, use a ternary operator to print:
Application acceptedor
Application rejectedbased on the admission result.