Try it: Flow Control#

Now it’s your turn to try out some of the techiques identified in the first few notebooks. For each example, take a look first at the question. Try to work out the logic in plain language first, then work out the coding logic afterward. The outcome is described after each of the steps. In this notebook, we’ll look at:

  • code blocks

  • conditional statements

  • looping statements

Number of code blocks#

How many code blocks are there in the following example?

for i in range(5):
    if i % 2: 
        print(f'{i} is an even number')
    else:
        print(f'{i} is an odd number')
    
    for j in range(10):
        print(j)

The number of code blocks is: ___________________

Hello World! revisited#

We’ll go back to the example from the first notebook where we introduced ourselves and with a twist. Our greeting is plesant, but we can do better. We should have a greeting that takes into account the time of day. The first few lines determine the hour of the day (where 0 is midnight and 23 is 11pm). Using an if/else construct, greet the user appropriately for the time of day (morning, afternoon, evening).

# Getting the current time
from datetime import datetime as dt
import pytz
current_hour = dt.now(pytz.timezone("US/Central")).hour
print(f'The current hour is: {current_hour}')

# Assign the variable to my_name
my_name = ...

# Ask for the user's name
user_name = input('What is your name?')

# Set the greeting to default to nothing
greeting = ''
# If the the current hour is less than 12, change the greeting to 'Morning'
# Otherwise, if the current hour is between 12 and 17 (5pm) set the greeting to 'Afternoon'
# Otherwise, set the greeting to 'Evening'

print(f'Good {greeting}, {user_name}!  My name is {my_name}')

Alphabet Soup#

For this challenge, using what you have learned about for loops and if statements to capitalize all the vowels (a,e,i,o,u) in the string provided and print the results. To help you out, the letters of the alphabet have been provided. The next cell provides an outline of the steps in English. Working through the logic in English first can make solving problems a bit easier. Try working through the steps one by one and translating each line into the Python code that executes the action. Bonus, leaving the comments in place essentially creates automated documentation for your code!

HINT: Remember strings are just a sequence of letters which can be iterated and also concatenated simply with +.

# This is the word for which you should be replacing the vowels
alphabet = 'abcdefghijklmnopqrstuvwxyz'

# create a variable to hold the new set of letters
# for each letter in the word provided
    #  if the letter is an `a` then add a captial `A` to the new set of letters
    #  otherwise if the letter is an `e` then add a capital `E` to the new set of letters
    #  otherwise if the letter is an `i` then add a capital `I` to the new set of letters
    #  otherwise if the letter is an `o` then add a capital `O` to the new set of letters
    #  otherwise if the letter is an `u` then add a capital `U` to the new set of letters
    #  otherwise add the current letter to the new set of letters
# print the new string