-->

Python 3.2.1.6 LAB: Essentials of the for loop - counting mississippily

Essentials of the for loop - counting mississippily

3.2.1.6 LAB

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.6 LAB: Essentials of the for loop - counting mississippil.

Scenario

Do you know what Mississippi is? Well, it's the name of one of the states and rivers in the United States. The Mississippi River is about 2,340 miles long, which makes it the second longest river in the United States (the longest being the Missouri River). It's so long that a single drop of water needs 90 days to travel its entire length!

The word Mississippi is also used for a slightly different purpose: to count mississippily.

If you're not familiar with the phrase, we're here to explain to you what it means: it's used to count seconds.

The idea behind it is that adding the word Mississippi to a number when counting seconds aloud makes them sound closer to clock-time, and therefore "one Mississippi, two Mississippi, three Mississippi" will take approximately an actual three seconds of time! It's often used by children playing hide-and-seek to make sure the seeker does an honest count.

Your task is very simple here: write a program that uses a for loop to "count mississippily" to five. Having counted to five, the program should print to the screen the final message "Ready or not, here I come!"

Use the skeleton we've provided in the editor.

EXTRA INFO

Note that the code in the editor contains two elements which may not be fully clear to you at this moment: the import time statement, and the sleep() method. We're going to talk about them soon.

For the time being, we'd just like you to know that we've imported the time module and used the sleep() method to suspend the execution of each subsequent print() function inside the for loop for one second, so that the message outputted to the console resembles an actual counting. Don't worry - you'll soon learn more about modules and methods.

Expected output

1 Mississippi

2 Mississippi

3 Mississippi

4 Mississippi

5 Mississippi

Solution Code

As usual, I highlighted the key things that will help you solve the code. So:

  • We need to to count to from One to Five as in the example-> so, range will be 1, 6 as 6 will be ignored and console will stop on 5.
  • The variable which will be counted (which will come after for is named mississippily ) This variable will be converted into a number in the console (from 1 to 5)
  • As in the Expected output given, we need to add the string "Mississippi" after the variable mississippily  to show in the console.
  • After each result, we need to hold/paus for 1 second inside the block of for.
  • As instructed, the final message (Final here means that the print() function will be OUTSIDE of the for block).

import time

for mississippily in range(1 , 6): # Write a for loop that counts to five.
    print(mississippily , "Mississippi") # Body of the loop
    time.sleep(1) #Pause for 1 second after each iteration in for loop
print("Ready or not, here I come!") # Final message


=================================================================================
Follow the blog to be the first to know ❤


Post a Comment

0 Comments