Skip to content

Commit

Permalink
IF section partially completed
Browse files Browse the repository at this point in the history
  • Loading branch information
stalj committed Sep 25, 2024
1 parent 9e028b4 commit f50c2cd
Show file tree
Hide file tree
Showing 3 changed files with 577 additions and 95 deletions.
13 changes: 11 additions & 2 deletions 02-TypesAndVariables/02-TypesAndVariables.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ [email protected]
py myprogram.py
```

Below is a program to calculate Body Mass Index. Run the program in Script mode. Hint: create a text file bmi.py and copy and paste the program below. Please note that each program line must start in the first column. Remove unnecessary spaces if necessary.
Below is a program to calculate Body Mass Index. Run the program in Script mode.

> Hint: create a text file bmi.py and copy and paste the program below. Please note that each program line must start in the first column. Remove unnecessary spaces if necessary.
```python
###
Expand All @@ -72,7 +74,9 @@ [email protected]

1. To create computer programs, it is most convenient to use an integrated environment (IDE). This tool allows you to both create a program and run it. One of the popular tools is Microsoft Visual Studio Code (VS Code).

The program below simulates five dice rolls. In VS Code, create and run the program. Hint: on the Windows desktop, create a new folder called IDE. Then, open the folder in VS Code. Next, in VS Code, in the IDE folder, create a new dice.py file. Finally, run the created program.
The program below simulates five dice rolls. In VS Code, create and run the program.

> Hint: on the Windows desktop, create a new folder called IDE. Then, open the folder in VS Code. Next, in VS Code, in the IDE folder, create a new dice.py file. Finally, run the created program.

```python
###
Expand Down Expand Up @@ -176,6 +180,11 @@ [email protected]

1. Two variables x and y have values of 7 and 34. Write a program that swaps variable values (x should be 34 and y should be 7). Use an additional, auxiliary variable.

> Hint: It is a good idea to always put a short description of the program in a comment at the beginning of the file. You can use the task text for this.

> Hint: The program file name may contain the section number and task number, e.g. 3-7.py.


```python
###
# A program for swapping two varable values
Expand Down
199 changes: 106 additions & 93 deletions 03-ControlStructures/ControlStructures.md
Original file line number Diff line number Diff line change
@@ -1,109 +1,122 @@
# CONTROL STRUCTURES

# Before Class
<!--
(c) Janusz Stal
Krakow University of Economics
Department of Informatics
[email protected]
-->

1. Find out what programming language statements are used to handle
decisions and performs computations and actions conditionally.
# CONTROL STRUCTURES

2. Read chapter 3 \"Conditional execution\" from the textbook.
## 1. Decision Statement

3. Watch the videos on using if-then-else conditional statements in
1. Watch the videos on using if-then-else conditional statements in
Python:

<https://youtu.be/FvMPfrgGeKs?feature=shared>

<https://youtu.be/Zp5MuPOtsSY?feature=shared>

<https://youtube.com/playlist?list=PLi01XoE8jYohWFPpC17Z-wWhPOSuh8Er->

4. How, in a computer program, it is possible to execute a program
statement multiple times. Familiarise yourself with loop statements
(for and while).

5. Watch the video on using the \"for\" statement in Python:

<https://youtu.be/94UHCEmprCY>

6. Find out what the term "debugging" means. Then watch the video
explaining how to test your program using the debugger.

<https://youtu.be/KEdq7gC_RTA>

<https://youtu.be/b4p-SBjHh28?feature=shared>

7. In the following program, mark breakpoints in lines 1, 5 and 7.
Then, do the tasks below:

a. Run the program in debug mode. Then, execute all program
statements, one by one. Observe the changing values of
variables.

b. Run the program in debug mode. Move between the marked
breakpoints.

c. Run the program in debug mode. Add the variable 'sum' and
'number' to the Watch window, and the expression number \<= 5.
Execute the program step by step. Observe the changes in the
variables and in the added expression.

sum = 0\
number = 1\
while number \<= 5:\
sum = sum + number\
number = number + 1\
message = f\"Sum of numbers in \<1,5\> is {sum}\"\
print(message)


## Conditional statement

8. The speed limit on a motorway in Poland is 140 km/h. Write a program
that checks whether a car exceeded the speed limit. If so, a warning
is displayed. Sample result:

speed_limit = 140\
car_speed = int( input(\'Enter car speed km/h: \') )\
\
if car_speed \> speed_limit:\
print(\'Warning: speed limit exceeded!!\')

9. A test is passed when the number of correctly completed tasks is at
> In your free time you can also watch other videos available on the Internet, such as:
<https://youtu.be/Zp5MuPOtsSY?feature=shared>
<https://youtube.com/playlist?list=PLi01XoE8jYohWFPpC17Z-wWhPOSuh8Er->
1. The speed limit on a motorway in Poland is 140 km/h. The following program
checks whether a car exceeded the speed limit. If so, a warning
is displayed. Complete the program.

```python
###
# Program that checks whether a car exceeded the speed limit
#
speed_limit = 140
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 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. Sample result:

Test passed

10. Write a program to calculate the absolute value of a number entered
from the keyboard. Sample result:
tasks are included in variables.

```python
###
# Program that checks whether the test is passed
# Test is passed when the number of correctly completed
# tasks is at least 50%
#
tasks = 20
tasks_ok = ...
if tasks_ok >= ... :
print('You passed the test!')
```

1. Write a program to calculate the absolute value of a number entered
from the keyboard.

```python
###
# Program for calculating absolute value
#
number = ...
if number < 0:
abs_number = ...
else:
abs_number = ...
print(f'|{...}| is {...}')
```

1. Write a program that checks whether the number entered from the
keyboard is even or odd.

```python
###
# Program that checks whether the number
# entered from the keyboard is even or odd
#
number = int(input('Enter number: '))
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.

```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.

```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')
```

Enter number: -17\
\|-17\| = 17

11. Write a program that checks whether the number entered from the
keyboard is even or odd. Sample result:

Enter number: 27\
Number is odd

12. Write a program that checks that two people are adults. Read
people's data from the keyboard. Sample result:

Enter first person name: Peter\
Enter first person age: 21\
Enter second person name: Ann\
Enter second person age: 18\
Both Peter and Ann are adults
## Loops

13. A user enters two integer numbers from the keyboard. Write a program
that checks whether at least one of them is not negative. Sample
result:
1. Watch the video on using the \"for\" statement in Python:

Enter number 1: 25\
Enter number 2: -17\
At least one of entered numbers 25 and -17 is not negative
<https://youtu.be/94UHCEmprCY>

## Loops

14. Write a program that displays the sentence \"Practice makes
perfect\" four times. Use the \"while\" statement.
Expand Down
Loading

0 comments on commit f50c2cd

Please sign in to comment.