Climate Quest - Task 7
Objectives
- Define iteration in programming and its importance.
- Recognize the concept of loops as a fundamental element of iteration.
- Differentiate between
for
andwhile
loops in Python. - Explain when to use a
for
loop and when to use awhile
loop. - Describe the purpose of the
range()
function infor
loops. - Illustrate the use of
for
loops to iterate over sequences like lists, tuples, and strings. - Explain the concept of nested
for
loops and demonstrate their use with examples. - Understand the structure of a
while
loop in Python. - Compare and contrast
for
loops andwhile
loops.
Task Overview:
In this homework, you will enhance the "Climate Quest" project by incorporating iteration. You will use for
and while
loops to handle repeated tasks and processes within the game. This will make the code more efficient and introduce the concept of looping through sequences and repeating actions.
Instructions:
-
Use
for
Loops to Iterate Over Sequences:- Implement
for
loops to handle repeated actions in the game, such as iterating over challenges.
- Implement
-
Use
while
Loops for Repeated Actions:- Implement
while
loops for conditions where the repetition depends on a condition, such as continuing the game until a certain condition is met.
- Implement
-
Nested
for
Loops:- Use nested
for
loops where appropriate, for example, when dealing with multiple elements within a challenge.
- Use nested
Detailed Steps:
Experiment with two different approaches to the game:
- The user visits a predetermined number of locations, say 3, as part of the game
- The user has to visit all of the locations before being presented with a final challenge
In both instances the main menu will have to be returned to and presented to the user, either for a set number of times, or until all locations have been visited
-
Use
for
Loops to Iterate Over Sequences:Update the main gameplay loop to use a
for
loop to iterate over a predefined number of challenges provided by therange()
function: -
Use
while
Loops for Repeated Actions:Repeat the previous exercise but this time using a
while
loop. For this exercise there will be 2 conditions:- while score > 0 and visited_locations < 6:
Adjust the
main()
function to display the menu while these two conditions areTrue
.
Example Homework Questions:
-
Use
for
Loops to Iterate Over Sequences:- Refactor your main gameplay loop to use a
for
loop to iterate over a predefined number of challenges.
- Refactor your main gameplay loop to use a
-
Use
while
Loops for Repeated Actions:- Modify your main gameplay loop to use a
while
loop to continue the game until the environmental health drops below a certain threshold.
- Modify your main gameplay loop to use a