From 3e49c3926a7c326a5d99e2859bafcce09d4dfca2 Mon Sep 17 00:00:00 2001 From: Janusz Stal Date: Sat, 28 Sep 2024 15:58:56 +0200 Subject: [PATCH] in the middle of loop section --- 02-TypesAndVariables/02-TypesAndVariables.md | 2 +- 03-ControlStructures/ControlStructures.md | 397 ++++++++++++------- 2 files changed, 246 insertions(+), 153 deletions(-) diff --git a/02-TypesAndVariables/02-TypesAndVariables.md b/02-TypesAndVariables/02-TypesAndVariables.md index a780378..c88a8c3 100644 --- a/02-TypesAndVariables/02-TypesAndVariables.md +++ b/02-TypesAndVariables/02-TypesAndVariables.md @@ -727,7 +727,7 @@ stalj@uek.krakow.pl print(f'You won: {...}') ``` -## 8. Problem Solving +## 8. Practice Makes Perfect 1. An algorithm is a step-by-step procedure or a set of rules designed to solve a problem. In computer science and mathematics, algorithms are critical because they provide a clear sequence of instructions that can be implemented to achieve a desired outcome. diff --git a/03-ControlStructures/ControlStructures.md b/03-ControlStructures/ControlStructures.md index b89e683..3813442 100644 --- a/03-ControlStructures/ControlStructures.md +++ b/03-ControlStructures/ControlStructures.md @@ -20,19 +20,14 @@ stalj@uek.krakow.pl 1. Decision statements in Python are used to make decisions based on logical conditions. The main decision-making structure is the if statement, which allows a block of code to be executed if a given condition is true. - Basic if Statement Syntax: + Familiarise yourself with the basic **if** statement syntax: ```python if condition: # code to execute if the condition is true - ``` - -1. Password length has a significant impact on the level of data protection. Write a program that checks whether the new password provided is at least 12 characters long. - - ```python - new_password = input('Enter new password: ') - if len(...) < ...: - print('Password too short (min. 12 chars)') + else: + # code to execute if the condition is false + # (note that else is optional) ``` 1. The speed limit on a motorway in Poland is 140 km/h. The following program @@ -41,30 +36,49 @@ stalj@uek.krakow.pl ```python ### - # Program that checks whether a car exceeded the speed limit + # Checking whether a car exceeded the speed limit # speed_limit = 140 - car_speed = int( input('Enter car speed km/h: ') ) - if car_speed > ...: + car_speed = int( input('Enter car speed (km/h): ') ) + if car_speed ...: print(f'Your speed is {...}km/h') print('Warning: speed limit exceeded!!') ``` +1. A payment terminal in a store allows card payments. The customer has money in his bank account. The total value of purchases is given. The terminal displays a message whether the payment can be made or whether there are no funds in the customer's bank account. Write a program that acts as a payment terminal. + + ```python + ### + # Credit card payment + # + account_balance = ... + total_payment = ... + if total_payment ... account_balance: + print('Payment completed') + else: + print('No funds') + ``` + 1. A test is passed when the number of correctly completed tasks is at - least 50%. Write a program that checks whether the test is passed. - The total number of test tasks and the number of correctly completed - tasks are included in variables. + least 50%. Write a program that checks whether the test is passed. Then use the program to check if you passed the test for the following number of properly performed tasks: + + 20, 11, 10, 9, 0 ```python ### - # Program that checks whether the test is passed + # Checking whether the test is passed # Test is passed when the number of correctly completed # tasks is at least 50% # - tasks = 20 + total_tasks = 20 tasks_ok = ... - if tasks_ok >= ... : - print('You passed the test!') + test_passed = False + if tasks_ok ... total_tasks ... : + test_passed = True + if test_passed: + print('Congratulations! You passed the test.') + else: + print('Unfortunately, you failed the test.') ``` 1. Write a program that checks whether the number entered from the @@ -72,85 +86,40 @@ stalj@uek.krakow.pl ```python ### - # Program that checks whether the number + # Checking whether the number # entered from the keyboard is even or odd # number = int(input('Enter number: ')) - if number % 2 == ...: + if number % 2 ...: print(f'{...} is even') else: print(f'{...} is odd') ``` -1. Write a program that checks that two people are adults. Read - people's data from the keyboard. +1. The employee's basic salary is PLN 5000. The employee may receive a bonus of several percent of the basic salary. Write a program that calculates the employee's salary, taking into account the possibility of receiving a bonus. Then calculate the employee's total salary for the following cases: - ```python - ### - # program that checks that two people are adults - # - person1_name = input('Enter first person name: ') - person1_age = int(input('Enter first person age: ')) - person2_name = ... - person2_age = ... - if person1_age >= 18 and person2 ...: - print('Both {person1_name} and {...} are ...') - else: - print('One of the two people is not an adult') - ``` - -1. A user enters two integer numbers from the keyboard. Write a program - that checks whether at least one of them is not negative. + * The employee does not receive a bonus + * The employee receives a bonus of 30% ```python ### - # Program that checks whether at least number entered - # from the keyboard is not negative - # - x = int(input('Enter first number: ')) - y = ... - if x ... or ... : - print(f'At least one of the numbers {} and {} is not negative') - ``` - -1. A payment terminal in a store allows card payments. The customer has X money in his bank account. The total value of purchases is Y. The terminal displays a message whether the payment can be made or whether there are no funds in the customer's bank account. Write a program that acts as a payment terminal. - - ```python - account_balance = ... - total_payment = ... - if total_payment ... account_balance: - print(...) + # Program that calculates the employee's salary, + # taking into account the possibility of receiving a bonus. + # + basic_salary = 5000 + is_bonus = True # does the employee receive a bonus + bonus = 0.15 # 15% + if ...: + total_salary = ... else: - print('Payment completed.') - ``` - -1. A lamp in a room has three light bulbs. Switch one lights one bulb, and switch two lights the other two bulbs. Write a program that tells you how many bulbs are lit, depending on the state of switch one and switch two. - - ```python - light_switch1 = False # False - switch off, True - switch on - light_switch2 = False - bulbs_on = 0 - if light_switch1: - bulbs_on += 1 - if light_switch2: ... - print(...) + print('Basic salary: {...}') + print('Bonus: {...}') + print('Total salary: {...}') ``` -1. Write a program that calculates and displays the quarter of the year for a given month number (1..12). Then check the program's results for the months: - - 12, 10, 9, 1 - - ```python - month = int(input('Enter month number (1..12): ')) - if month >= 10: - quarter = 4 - elif ...: - ... - print(...) - ``` -## Multiple Conditions +## 2. Multiple Conditions 1. Objaśnienie, link do materiałów w necie. @@ -180,37 +149,11 @@ stalj@uek.krakow.pl ... ``` -1. The electronic thermometer displays the temperature in degrees Celsius and verbal information according to the following criteria: - - * It is extremely hot, for a temperature above 35 degrees, - * It is hot, for a temperature above 30 degrees, - * It is warm, for a temperature of at least 15 degrees, - * It is cold, for a temperature of at least 0 degrees, - * Warning, frost, for a temperature below 0 degrees. - - Write a program that simulates the operation of an electronic thermometer. Then check the correctness of the program for the following temperatures in degrees Celsius: - - 33, 30, 8, 0, -2 - - ```python - ### - # Program that simulates the operation of an electronic thermometer. - # - temperature = 32 - if temperature > 35: - print("It is extermely hot") - elif temperature > 30: - print ... - elif ... - ``` - -1. The parking meter calculates the parking fee based on the number of hours the car was parked according to the following rules: - - * 1-2 hours: 5 PLN - * 3-6 hours: 15 PLN - * Over 6 hours: 20 PLN +1. Integer Analyzer. Write a program that asks the user to enter an integer and checks and displays the integer information: - Write a program that asks for the number of hours of parking, then calculates and displays the correct fee. + * Number ... is positive + * Number ... is negative + * Number is 0 1. The car has three driving modes: Auto (A), Manual (M) and Eco (E). In each of these three modes, the average fuel consumption in liters per 100 km is 7, 9 and 6 respectively. Write a program that calculates total fuel consumption for a given distance in km and a given driving mode. @@ -227,17 +170,20 @@ stalj@uek.krakow.pl 1. Simple Calculator. Write a program that asks the user to enter a symbol of mathematical operation (+, -, *, /) and two numbers. The program should perform the appropriate mathematical operation on the given numbers and return the result. +1. Write a program that calculates and displays the quarter of the year for a given month number (1..12). Then check the program's results for the months: -1. Integer Analyzer. Write a program that asks the user to enter an integer and checks and displays the integer information: - - * Number ... is positive - * Number ... is negative - * Number is 0 - - + 12, 10, 9, 1 + ```python + month = int(input('Enter month number (1..12): ')) + if month >= 10: + quarter = 4 + elif ...: + ... + print(...) + ``` -## Logical Operators +## 3. Logical Operators 1. You can combine conditions using logical operators like and, or, and not: @@ -248,6 +194,9 @@ stalj@uek.krakow.pl 1. Write a program that checks whether the entered login and password are correct. ```python + ### + # Checking login and password + # login = 'joe' password = 'abcd' entered_login = input('Login: ') @@ -258,25 +207,16 @@ stalj@uek.krakow.pl print('Incorrect login or password!!') ``` -1. Write a program that calculates and displays the number of days in a given month (1..12). Assume that February always has 28 days. - - ```python - month = int(input('Enter month number (1..12)')) - if month==1 or month==3 or ... : - days = 31 - elif ... : - days = ... - ... - print('Month ... has ... days') - ``` - -1. Napisz program, który zapyta użytkownika o jego wiek, a następnie sprawdzi, do której grupy wiekowej należy: Dziecko: poniżej 13 lat, Nastolatek: od 13 do 19 lat, Dorosły: od 20 do 64 lat, Senior: 65 lat lub więcej - 1. The discount is available to children under 18, or people 65 or older. Write a program that checks whether a person of a given age is eligible for the discount. Then use the program to check if people of the age listed below are eligible for a discount. 72, 65, 64, 18, 17 ```python + ### + # Checking if discount is available + # The discount is available to children under 18, + # or people 65 or older. + # age = int(input('Enter your age: ')) if age < 18 or ... : print(...) @@ -284,32 +224,90 @@ stalj@uek.krakow.pl print(...) ``` -1. Write a program that checks if the given number of days of the month is correct. +1. A user enters two integer numbers from the keyboard. Write a program + that checks whether at least one of them is not negative. + + ```python + ### + # Program that checks whether at least one number entered + # from the keyboard is not negative + # + x = int(input('Enter first number: ')) + y = ... + if not x < ... or ... : + print(f'At least one of the numbers {} and {} is not negative') + ``` + +1. Write a program that calculates and displays the number of days in a given month (1..12). Assume that February always has 28 days. + + ```python + ### + # Calculates the number of days in a month + # + month = int(input('Enter month number (1..12)')) + if month==1 or month==3 or ... : + days = 31 ## months with 31 days + elif ... : + days = ... ## months with 30 days + ... + ## February 28 days + print('Month ... has ... days') + ``` + +1. Write a program that checks if the given day number of the month is correct. Then, run the program for the following data: + + * month 3, day 17 + * month 9, day 31 + * month 2, day 30 ```python month = int(input('Enter month number (1..12): ')) - days = int(input('Enter the number of days: ')) + day = int(input('Enter the day number of the month: ')) + day_ok = False if month==1 or month==3 or ...: - if days >=1 and days <= 31: - print('The given number of days ... for the month ... is correct') + if day >=1 and day <= 31: + day_ok = True elif month== ...: - if days >=1 and days <= ...: + if day >=1 and day <= ...: ... ... + print('Day {...} for the month {...}', end="") + if day_ok: + print(' is correct') + else + print(...) ``` +## 4. Iteration Over a Sequence +1. Watch the video on using the \"for\" statement in Python: + +1. The **for** loop is used to iterate over a sequence and execute a block of code for each item in the sequence. + Here is the basic syntax of the **for** statement: -## Loops + ```python + for item in sequence: + # code block to execute + ``` -1. Watch the video on using the \"for\" statement in Python: + Look at the following example, which takes every character from a string and displays it on the screen. Then run the program. - + ```python + city = 'Krakow' + for char in city: + print(char) + ``` + You can use range() to loop over numbers: + ```python + for i in range(5): + print(i) + ``` + 14. Write a program that displays the sentence \"Practice makes perfect\" four times. Use the \"while\" statement. @@ -349,20 +347,115 @@ stalj@uek.krakow.pl 19. Write a program that calculates the sum of even numbers in the range \<1,10\>. -## Debugging +## 5. Conditional Iteration + +1. The ***while** loop executes a block of code as long as a specified condition is True. + + Here is the basic syntax of the **while** statement: + + ```python + while condition: + # code block to execute + ``` + + Look at the following example. Then try to run the program. + + ```python + count = 0 + while count < 5: + print(count) + count += 1 + ``` + +## 6. Practice Makes Perfect + +1. A lamp in a room has three light bulbs. Switch one lights one bulb, and switch two lights the other two bulbs. Write a program that tells you how many bulbs are lit, depending on the state of switch one and switch two. + + ```python + ### + # House lighting with three bulbs and two switches + # Checking how many bulbs are illuminating the house + # + light_switch1 = False # False - switch off, True - switch on + light_switch2 = False + bulbs_on = 0 + if light_switch1: + bulbs_on += 1 + if light_switch2: + ... + print(...) + ``` + +1. Password length has a significant impact on the level of data protection. Write a program that checks whether the new password provided is at least 12 characters long. + + ```python + ### + # Password validator + # New password is at least 12 characters long + # + new_password = input('Enter new password: ') + if len(...) < ...: + print('Password too short (min. 12 chars)') + ``` + +1. The electronic thermometer displays the temperature in degrees Celsius and verbal information according to the following criteria: + + * It is extremely hot, for a temperature above 35 degrees, + * It is hot, for a temperature above 30 degrees, + * It is warm, for a temperature of at least 15 degrees, + * It is cold, for a temperature of at least 0 degrees, + * Warning, frost, for a temperature below 0 degrees. + + Write a program that simulates the operation of an electronic thermometer. Then check the correctness of the program for the following temperatures in degrees Celsius: + + 33, 30, 8, 0, -2 + + ```python + ### + # Program that simulates the operation of an electronic thermometer. + # + temperature = 32 + if temperature > 35: + print("It is extermely hot") + elif temperature > 30: + print ... + elif ... + ``` + +1. The parking meter calculates the parking fee based on the number of hours the car was parked according to the following rules: + + * 1-2 hours: 5 PLN + * 3-6 hours: 15 PLN + * Over 6 hours: 20 PLN + + Write a program that asks for the number of hours of parking, then calculates and displays the correct fee. + +1. Napisz program, który zapyta użytkownika o jego wiek, a następnie sprawdzi, do której grupy wiekowej należy: Dziecko: poniżej 13 lat, Nastolatek: od 13 do 19 lat, Dorosły: od 20 do 64 lat, Senior: 65 lat lub więcej + +1. Write a program that checks that both people are adults. Read + people's data from the keyboard. + + ```python + ### + # Checking if both people are adults + # + person1_name = input('Enter first person name: ') + person1_age = int(input('Enter first person age: ')) + person2_name = ... + person2_age = ... + if person1_age >= 18 and person2 ...: + print('Both {person1_name} and {...} are ...') + else: + print('Either {...} or {...} is not an adult') + ``` + + + + + -20. The following program calculates the sum of the integers in the - range 1 to 5. Run the program in debug mode and try to analyse the - program execution. See how you can execute the program step by step - and track changes in variable values. - sum = 0\ - for i in range(1,6):\ - print(i)\ - sum = sum + i\ - print(f\'Sum is {sum}\') -# After Class 21. Write a program that displays two numbers entered from the keyboard in ascending order.