6.11. Exercises: Loops¶
Loops simplify repetitive tasks!
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.
- 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.
- 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
indexis an integer value, and
var_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 and
range(start, stop, step).
Hint: Instead of figuring out the
stopvalue by counting all of the characters in
gibberishyourself, make Python do it for you! Recall that
len(gibberish)returns the length of the string stored in the variable.
Repeat the previous problem, but:
range(start, stop, step)with
- Use an
ifstatement and the modulus (
%) operator to check if the index is divisible by 5.
True, print the character. If
False, do not print the character.
- The output should be the same as before.
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.
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
- 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
- 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
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."
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¶
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
end_value. Use the variable
totalas the accumulator. Print
"The sum of the numbers from ___ to ___ is ___."Fill in the blanks with the values for
- 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.
string = "1234"and
other_string = "5678", then the output will be
code_1 = "ABCDEF"and
code_2 = "notyet", then the output will be
ka = "LoOt"and
blam = "oku!", then the output will be