4. Flow control#

Exercise 4.1.#

Create a variable named hour and asign it a value in between 0 and 23. Assuming that this variable represents a given time, print a message that is appropriate for the part of the day this hour is in. If the variable hour has value 10, for instance, the code should print 'Good morning!' and if hour has value 23, the code should print 'Good night!'

hour = 21

print( f"It is now {hour} o'clock" )

# We check for two conditions at once using `and`
if hour > 6 and hour < 12:
    print("Good morning!")
elif hour < 17:
    print("Good afternoon!")
elif hour < 23:
    print("Good evening!")
else:
    print("Good night!")

Note that we expect the value of hour to be reasonable for our code to work as expected. For example, we don’t check whether hour is at least 0 or at most 23. It is advisable to include these checks in code that you rely on.

In case you wondered if you can make the comparisons more compact, you can! You can compare a number to two numbers at the same time, as demonstrated below.

Exercise 4.2.#

Strings can be viewed as collections of characters. When you iterate across a string using a for loop, you will receive each individual character in sequence. Try to write code which print all each characters of a string on a separate line.

text = 'Flow control'

for t in text:
    print(t)

Exercise 4.3.#

Define a list ranging from 1 to 100 using the range() function. Next, print the cumulative sum of all the numbers in this collection of numbers.

cumulative_sum = 0

for i in range(1,101):
    cumulative_sum += i
    
print(cumulative_sum)

Exercise 4.4.#

Building on the code you developed for exercise 4.2, try to write code which can reverse the order of the characters in a given string.

You can test your code with the following examples:

Input

Output

time

emit

drawer

reward

stressed

desserts

input_string = 'drawer' 
output_string = ''

for character in input_string:
    output_string = character + output_string 
    
print(output_string)

Exercise 4.5.#

Program a small guessing game. In this game, the player needs to guess a number in between 1 and 50. You can use the code below as a basis.

The randint() method from random can be used, firstly, to generate a random integer.

Use the function input() to request a value from the user. The function int() needs to be used to convert the input into an integer.

When the user enters a value which is too low or too high, this information is communicated to the user via a print statement. To test whether the number that is guesses is correct, you can work with the equality (==) and the inequality (!=) operators.

Hint: you will need to combine the different kinds of flow control to complete the whole game. Start by checking if the guessed number is correct and making sure that this part of the code works as you expect. When that works, think which parts of the code need to be repeated to let the player guess until the number was found.

from random import randint

max_number = 50
number_to_guess = randint(0,max_number)

guess = int( input( f"Guess a number in between 1 and {max_number}: ") )

while guess != number_to_guess:

    if guess > number_to_guess:
        print("Lower!")
    elif guess < number_to_guess: # this could be 'else:'
        print("Higher!")
    guess = int( input("Guess again ... \n") )

print( f"The correct number is indeed {number_to_guess}." )