During session 4
Exercises
1. Warm-up: password
Write a program which prompts the user for a password. The program has to check if the entered password is correct or not. If the password is correct, the program greets the user; otherwise, the program prints out the following message:
Ask the password from the user or pick up Python!
NB! There are only three attempts to enter the password. If the password is entered three times incorrectly, the program prints out the following message:
Too many attempts!
Then the program finishes the execution.
Test your program!
Test 1 (correct password is "test")
Enter the password: test Welcome!
Test 2 (correct password is "test")
Enter the password: asd Ask the password from the user or pick up Python! Enter the password: test Welcome!
Test 3 (correct password is "test")
Enter the password: asd Ask the password from the user or pick up Python! Enter the password: zxc Ask the password from the user or pick up Python! Enter the password: qwe The password is incorrect. Too many attempts!
2. Triangle
Write a program which prompts the user for a number. The program then prints out a triangle whose both legs are equal to the entered number.
Enter a number of rows: 7 * ** *** **** ***** ****** *******
Hint:
>>> print('*' * 3)
***
>>> print('*' * 6)
******
3. Integers that are not divisible by 10
Write a program which prompts the user for two numbers (for example, m and n) and prints out all integers from that range that are greater than or equal to m and less than n and are not divisible by 10.
Here is an example of the program output.
Enter number m: 8 Enter number n: 12 8 9 11
Hint:
>>> 14%10
4
>>> 10%10
0
>>> 25%10
5
>>> 20%10
0
4. Celsius and Fahrenheit
Write a program which contains a list of temperatures in Celsius degrees:
celsius = [32, 10, 15.6, 24, 5.7]
The program converts the temperatures to Fahrenheit and prints the results out using a definite loop (for).
Here is an example of program output:
Celsius: 32 Fahrenheit: 89.6 Celsius: 10 Fahrenheit: 50.0 Celsius: 15.6 Fahrenheit: 60.08 Celsius: 24 Fahrenheit: 75.2 Celsius: 5.7 Fahrenheit: 42.26
Hint:
Have a look at the video again.
5. The three-year child simulator
Write a program which asks the user one question. Every time the users enters an answer, the program asks "But why?" until the user enters a "magic word".
Here is an example of program output.
Why is grass green? Grass contains a special pigment that gives it a green color. This pigment is called chlorophyll. But why? Grass is green because it has chlorophyll. Chlorophyll is the green pigment that absorbs sunlight during photosynthesis. But why? Because But why? aaa But why? magic