10.1. Solutions to Chapter 3 Exercises#

10.1.1. Exercise 1#

Write an expression that will find the value of this arithmetic expression:

\[2\times(5+3)+4\times \dfrac{128}{ (\dfrac{16}{8})}-3^2\times (5\%2)\]
# Answer
2*(5+3)+4*(128/(16/8))-3*3*(5%2)
263.0

10.1.2. Exercise 2#

Write an expression that given the following values of x = True, y = False and z = False will find out the value of the following logical expression:

\[x \; AND \; (y \; OR \; z \; OR \; (x \; AND \; z))\]
# Write your code here
x = True
y = False
z = False
x and (y or z or (x and z))
False

10.1.3. Exercise 3#

Now we will modify the code from the Readings section 3.1.2.3. The user will input a grade and based on the ETH grading table you have to output the corresponding description. Find the details here. If the user inputs a number outside of the specified range then print this statement Incorrect grade!.

Since this depends on a user input, we will assume that the user has entered 6. Except of the grade assignment statement everything else is the same.

grade = 6

# Answer
if grade>6 or grade<1:
    print('Incorrect grade!')
elif grade>=5.75 and grade<=6.0:
    print('Excellent')
elif grade>=5.25 and grade<=5.5:
    print('Very Good')
elif grade>=4.75 and grade<=5.0:
    print('Good')
elif grade>=4.25 and grade<=4.5:
    print('Satisfactory')
elif grade==4.0:
    print('Sufficient')
else:
    print('Insufficient')
Excellent

10.1.4. Exercise 4#

We have a list of fermented food, but we do not like everything that is there. More precisely the list contains the following items: milk, yoghurt, beer, cider, tempeh, sauerkraut and kefir. We do not like: milk, cider and kefir. So we need to print everything except of these 3 foods. Write a program to accomplish this using a while loop.

fermented_foods = ['milk', 'yoghurt', 'beer', 'cider', 'tempeh', 'sauerkraut', 'kefir']
# Answer
i = 0
# because there are 7 elements we need to iterate 7 times
# since indexes start from 0, i will take values from
# 0 to 6.
while(i<7):
    i = i + 1
    if fermented_foods[i-1]=='milk':
        continue
    if fermented_foods[i-1]=='cider':
        continue
    if fermented_foods[i-1]=='kefir':
        continue
    print(fermented_foods[i-1], end=' ')
yoghurt beer tempeh sauerkraut 

10.1.5. Exercise 5#

Repeat exercise 4 using a for loop this time but when we encounter sauerkraut we do not want to continue looping anymore.

# Answer
for food in fermented_foods:
    if food=='milk':
        continue
    if food=='cider':
        continue
    if food=='kefir':
        continue
    if food=='sauerkraut':
        break
    print(food, end=' ')
yoghurt beer tempeh 

10.1.6. Exercise 6 (Optional - Bonus)#

Using a loop structure (either for or while) draw a Christmas tree with a base of 5 stars using asterisks(*). The output should look like this:

christmas_tree_illustration
# Answer
length = 5
for i in range(0,length):
    for j in range(0,(i+length)):
        if j>=(length-(i+1)):
            if (j%2==0 and i%2==0) or (j%2==1 and i%2==1):
                print('*', end='')
            else:
                print(' ', end='')
        else:
            print(' ', end='')
    print(' ')
    * 
   * * 
  * * * 
 * * * * 
* * * * *