Program 6: Detention Lines (Enhanced)

This is an enhanced version of the Detention Lines program. This time you can enter a sentence of your own and an amount of lines.

You can see the code and output below, or you can access it on repl.it by clicking here.

Code

Output

New Constructs/Concepts

This program doesn’t reveal any new constructs, however it shows some of the more advanced features of the Fixed Loop (for statement).

Variable as a limit

In this version of the program, instead of a given number, we use the contents of a variable (lines). A fixed loop can take a literal number or a integer variable as its limit (the amount of times it will repeat).

The ‘index’ variable

As you can see on line 9, inside our fixed loop, we concatenate a variable called ‘index’ with the sentence entered by the user.

The ‘index’ variable keeps track of how many times the loop has been round, starting at 0. So on the first iteration, index is 0, on the second, index is 1, etc.

Programmers can choose to call the index variable whatever they like, counter and index are the most common choices. Examples:

  • for index in range(10)
  • for counter in range(10)
  • for iteration in range(10)
  • for x in range(1)
  • for i in range(10)

Parsons Problem

Hint: Remember that in order to use a time delay, the first line of the program needs to import the time package.

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.