Control Flow: The if, elif, else Statements

Additional Practice

Rewrite the calculateFare function using nested conditional statements by first checking the distance, then checking the traveler type.

def calculateFare(distance, student, teacher, senior): 
    ...
    

Draw a simple mandala using squares and altering colors

Increase the number of colors, change the size

Plan and implement your solution before you take a look at the given code:

import turtle

def draw_square(side):
    for _ in range(4):
        turtle.forward(side)
        turtle.left(90)


turtle.speed(20)
numberOfSquares = 100

for i in range(numberOfSquares):
    if (i % 5 == 0):
        turtle.pencolor("Red")
    elif (i % 5 == 1):
        turtle.pencolor("Purple")
    elif (i % 5 == 2):
        turtle.pencolor("Blue")
    elif (i % 5 == 3):
        turtle.pencolor("Green")
    else:
        turtle.pencolor("Yellow")
    draw_square(100)
    turtle.left(360 / numberOfSquares)

turtle.done()


Draw a smaller mandala inside the larger one

import turtle


def draw_square(side):
    for _ in range(4):
        turtle.forward(side)
        turtle.left(90)


def draw_mandala(size, number_of_squares):
    for i in range(number_of_squares):
        if (i % 5 == 0):
            turtle.pencolor("Red")
        elif (i % 5 == 1):
            turtle.pencolor("Purple")
        elif (i % 5 == 2):
            turtle.pencolor("Blue")
        elif (i % 5 == 3):
            turtle.pencolor("Green")
        else:
            turtle.pencolor("Yellow")
        draw_square(size)
        turtle.left(360 / number_of_squares)


turtle.speed(20)
draw_mandala(100, 100)
draw_mandala(50, 100)

turtle.done()

Draw a spiral mandala by adjusting the size of the squares.

import turtle


def draw_square(side):
    for _ in range(4):
        turtle.forward(side)
        turtle.left(90)


turtle.speed(20)
number_of_squares = 300

for i in range(number_of_squares):
    if (i % 5 == 0):
        turtle.pencolor("Red")
    elif (i % 5 == 1):
        turtle.pencolor("Purple")
    elif (i % 5 == 2):
        turtle.pencolor("Blue")
    elif (i % 5 == 3):
        turtle.pencolor("Green")
    else:
        turtle.pencolor("Yellow")
    draw_square(10 + i)
    turtle.left(900 / number_of_squares)

turtle.done()

This code draws the squares starting from the smaller, can you modify a single line of the source code to begin with the larger square, so that smaller ones are not overdrawn?