During session 4
Exercises
1. Mantra
Mantra is a word or sound that is repeated as a prayer or to help people meditate. Mantra is repeated as many times as it as necessary.
Write a program which
- prompts the user for a sentence to be repeated in his mantra
- prompts the user for a number of times the mantra to be repeated
- prints out the entered mantra for the number of times specified by the user.
Here is an example of the program output:
Enter your mantra: om manu padme hum How many times to repeat: 3 om manu padme hum om manu padme hum om manu padme hum
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.
Here is an example of the program output:
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 all integers from the range [m ,n) (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.8]
The program has to convert the temperatures to Fahrenheit and print the results using a definite loop (for).
Here is an example of the 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.8 Fahrenheit: 42.44
Hint:
Have a look at the video again on the 2nd minute.
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 the word "magic".
Here is an example of the 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
6*. The soup is getting cold
Every minute the soup in the pot is getting cold by 19 percent of the difference between the soup and the indoor temperatures. Write a program which calculates the soup temperature for every minute. The initial soup temperature is 90 Celsius degrees and the indoor temperature is 20 Celsius degrees.
Do not let the program fall into an infinite loop!
Here is an example of the program output (first 5 lines).
1 min. Soup temperature: 76.7 2 min. Soup temperature: 65.927 3 min. Soup temperature: 57.20087000000001 4 min. Soup temperature: 50.132704700000005 5 min. Soup temperature: 44.407490807