# CS 101 Lab: Week # 4
The following have been covered during the 4th week of CS101 labs.
---
## Outline:
1. At first: short sum-up of the last lab, with references to what have been covered in the last 2 lectures by Prof. Arya,
2. Discovering boolean values (`True` or `False`), and boolean operations (`and`, `or`, `not`), *[ass.#1]*
3. Comparisons (`<`, `>`, `==`, `!=`, `<=`, `>=`). Warning with the equality: `==` and NOT `=` (which is for assigning values), *[ass.#2]*
4. (again) Getting values from the standard input from the user (with your keyboard), with `input` or `raw_input`, for more sophisticated example: `complex` numbers, `bool` values,
5. Comparisons for numbers, e.g. the nice shortcut `xmin <= x <= xmax`. Examples, with the first problem from Prof. Dhande's homework, *[ass.#3]*
6. The [**If** block (`if`)](https://docs.python.org/2/tutorial/controlflow.html#if-statements), first without an `else` block.
7. Introducing the `else` block,
8. Presenting the `elif` block, with one or more cases,
9. Using `input`, comparisons, and `if` blocks to write more interesting programs. *[ass.#4]*
### Bonus (if the time allows it)
1. *Bonus:* Explanations about how to write Python *blocks* with the proper *indentation*,
2. *Bonus:* Using the *Python* [documentation](https://docs.python.org/2/) (within [*Spyder IDE*](https://pythonhosted.org/spyder/)) to discover more about functions for complex numbers in the [cmath](https://docs.python.org/2/library/cmath.html) module, to improve your solution to *[ass.#5]* of last assignment sheet (with the [abs](https://docs.python.org/2/library/functions.html?highlight=abs#abs) and [phase](https://docs.python.org/2/library/cmath.html#cmath.pĥase) functions),
3. *Bonus:* Use a **While** (`while`) loop for checking that an input is valid, by looping (and asking with `input`) as long as necessary, *[bonus ass.#6]*
4. *Bonus:* Remarks on [the coding style](https://docs.python.org/2/tutorial/controlflow.html#intermezzo-coding-style). Try to understand what Python means when you do `import this`.
---
## Assignments (for next lab):
1. Print the tables of results for the 3 boolean operations (`and`, `or`, `not`). You should print something like this (here for `and`):
True and True : True
True and False : False
False and True : False
False and False : False
2. Write a 6-line program which print an example of results of the six binary comparison operators (`<`, `>`, `==`, `!=`, `<=`, `>=`). You can try to produce an output like this (but with the *examples of your choice*):
2 < 3 : True
1 > 0 : True
2**12 == (2**6)*(2**6) : True
(2+4)*5 != 2+(4*5) : True
10 <= 3 : False
1024 >= 2048 : False
3. Python solution for the [first problem from Prof. Dhande's](http://www.mahindraecolecentrale.edu.in/portal/pluginfile.php/970/mod_folder/content/0/CS101_Homework1__solution_Exercise1.py?forcedownload=1) homework, (as [uploaded on the Moodle already](http://www.mahindraecolecentrale.edu.in/portal/mod/folder/view.php?id=322)). Use a `if` block for something simple.
4. Create a program to first ask the user to enter a number in complex-number format (e.g. `z = 4 + 5j`). If the user enters a pure real number , then print *"Purely Real"*, else if the user enters a pure imaginary number, print *"Purely Imaginary"*, and if neither, evaluate the modulus and print *"Modulus of the complex number is"* and the actual value. Run the program 3 times with different inputs so that the control flows over each branch of the code,
5. (again) Interactive program to convert a temperature from °C to either °F or °K ([Kelvin degrees](https://en.wikipedia.org/wiki/Kelvin) or [Fahrenheit degrees](https://en.wikipedia.org/wiki/Fahrenheit)), but now that program have to ask the user which unit he wants, and print only °F or °K according to his choice (You can use the conversion formula **F = (9/5) * C + 32**, and **K = C + 273.15**).
6. *Bonus:* Improve problem 5. by checking (with the `assert` keyword, or by asking a new input as long as necessary with a `while` loop) that the input temperature is a valid temperature (**C > -273.15**). You can also try to check that the choice of the user is either `"K"` or `"F"`, and ask the question as many time as necessary (with a `while` loop).
7. An employee works in a firm where his daily earning is the product of his *"number of hours per day"* multiplied by *"hourly rate"*. Write a program that takes from the user (you) the number of hours as 7.5 and the hourly rate as Rs. 100. Then modify the program so that if the number of hours worked is more than 7.5, his hourly rate doubles (for overtime). But the doubling is done only for the number of hours worked beyond 7.5. Further, accommodate in your program the possibility of silly mistakes being made by the data entry staff, so that it will automatically identify these mistakes and revert back to the user with a statement to check his inputs. Hint: Identify and codify what range of earning can be considered as reasonable and beyond these, either side, will be considered as junk.
8. *Bonus:* Enhance the program written in the previous problem with the checks so that it does not crash, but reverts back elegantly to the user, in case the user enters any input that is not an integer or a float. Avoid adding any more IF statements. You can try to look into the Python documentation for an introduction to the [while loop](https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming) (which will be covered in next week lectures).
---
## Example program (for the assignment #3)
# -*- coding: utf-8 -*-
print "Problem #1 : check if a point is in a square work space [-100, 100] x [-100, 100]."
xmin, xmax = -100, 100
ymin, ymax = -100, 100
x = input("Give me the x coordinate for P : x = ")
y = input("Give me the y coordinate for P : y = ")
if (xmin <= x <= xmax) and (ymin <= y <= ymax):
print "Yes, the point P is inside the workspace :) !"
else:
print "No, the point P is not inside the workspace :( ..."
---
## Example program (for the temperature assignment #5)
# -*- coding: utf-8 -*-
print "I will convert a temperature from °C to °F:"
temp_C = float(raw_input("Please give me a temperature in °C: T = "))
assert -273.15 < temp_C
while True:
choice = raw_input("Choose between F or K: ")
if choice == "F":
temp_F = (9./5.) * temp_C + 32
print "So this temperature of", temp_C, "°C, is ", temp_F, "°F."
break
elif choice == "K":
temp_K = temp_C + 273.15
print "So this temperature of", temp_C, "°C, is ", temp_K, "°K."
break
else:
print "Your input", choice, "is not valid. Try again!"
---