6.11. Exercises: Loops¶
Loops simplify repetitive tasks!
Image from Bart’s Blackboard, Season 2, Episode 11 (Jan 24, 1991)¶
6.11.1. for Practice¶
Note
If your teacher added you to a Trinket course, complete the exercises there.
Otherwise, use the links below to code in your own free account.
Construct
forloops that accomplish the following tasks: (repl.it code or Trinket code)Print the numbers 0 - 20, one number per line.
Print only the ODD values from 3 - 29, one number per line.
Print the EVEN numbers 12 down to -14 in descending order, one number per line.
Print the numbers 50 down to 20 in descending order, but only if the numbers are multiples of 3.
Given the string
'LaunchCode', code twoforloops to do the following: (repl.it or Trinket)Print out each character of the string, one letter per line. Do this WITHOUT using index values.
Now use index values to print each character of the string—in reverse order—to a new line. Recall that you can access a single character from a string with the syntax
var_name[index], whereindexis an integer value, andvar_nameis the variable used to store the string.
Given the string
gibberish = 'Vna#hewzB*rQhT%yq^lv %iPwgOexWo &C^oUoGSdtJLj', print every fifth character, including the first character. Use index values andrange(start, stop, step).Hint: Instead of figuring out the
stopvalue by counting all of the characters ingibberishyourself, make Python do it for you! Recall thatlen(gibberish)returns the length of the string stored in the variable.
6.11.1.1. Bonus¶
Repeat the previous problem, but:
Replace
range(start, stop, step)withrange(len(gibberish)).Use an
ifstatement and the modulus (%) operator to check if the index is divisible by 5.If
True, print the character. IfFalse, do not print the character.The output should be the same as before.
6.11.2. while Practice¶
Define three variables for a spacecraft—one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the spacecraft reaches. Assign each variable an initial value of 0.
While loop starter code: repl.it or Trinket.
Construct
whileloops to do the following:Ask the user for the starting fuel level. The loop should continue until the user enters a value between 5000 and 30000. If the user submits a number outside of the range, print
"Invalid entry."Use a second loop to prompt the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7. For numbers outside of the range, print
"Invalid entry."
Use a third
whileloop to update the fuel and the altitude of the spacecraft. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers.Hint: The loop should end when there is not enough fuel to boost the crew another 50 km, so the fuel level might not reach 0.
After the loops finish, print the result using the phrase,
The spacecraft gained an altitude of ___ km and has ___ kg of fuel left.Fill in the blanks with the altitude and fuel level values.If the altitude is 2000 km or higher, add
"Orbit achieved!"to the output. Otherwise add,"Failed to reach orbit."
Sample Output
Enter starting fuel level (5000 - 30000): 22000
Enter the number of astronauts (1 - 7): 3
The spacecraft gained an altitude of 3650 km and has 100 kg of fuel left. Orbit achieved!
6.11.3. The Accumulator Pattern¶
Accumulator starter code: repl.it or Trinket.
Use two input statements to prompt the user for a start_value and an
end_value. Both inputs should be integers.
Use a loop to add up all of the numbers from
start_valuetoend_value. Use the variabletotalas the accumulator. Print"The sum of the numbers from ___ to ___ is ___."Fill in the blanks with the values forstart_value,end_value, andtotal.Define a variable to hold the string
'It was a bright cold day in April, and the clocks were striking thirteen.'Use the accumulator pattern to build a new string. It should contain all of the characters in the original string, but without any vowels. For this task, y does NOT count as a vowel. Print the new string.
Check your solutions. Challenge ———
If our spacecraft gets hijacked by space pirates, the astronauts can activate a self-destruct sequence to provide some drama for the viewers at home.
In order to prevent a rogue astronaut from activating the code, it takes two crew members to begin the countdown. Each person must enter a different code, after which the computer will “zip” them together before overloading the engines.
In a new code file, construct a loop that combines two strings together, alternating the characters from each source. For now, be careful to make both strings the same length.
Examples
If
string = "1234"andother_string = "5678", then the output will be"15263748".If
code_1 = "ABCDEF"andcode_2 = "notyet", then the output will be"AnBoCtDyEeFt".If
ka = "LoOt"andblam = "oku!", then the output will be"LookOut!".
