
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
forandwhileloops in Python. - Explain when to use a
forloop and when to use awhileloop. - Describe the purpose of the
range()function inforloops. - Illustrate the use of
forloops to iterate over sequences like lists, tuples, and strings. - Explain the concept of nested
forloops and demonstrate their use with examples. - Understand the structure of a
whileloop in Python. - Compare and contrast
forloops andwhileloops.
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
forLoops to Iterate Over Sequences:- Implement
forloops to handle repeated actions in the game, such as iterating over challenges.
- Implement
-
Use
whileLoops for Repeated Actions:- Implement
whileloops for conditions where the repetition depends on a condition, such as continuing the game until a certain condition is met.
- Implement
-
Nested
forLoops:- Use nested
forloops 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
forLoops to Iterate Over Sequences:Update the main gameplay loop to use a
forloop to iterate over a predefined number of challenges provided by therange()function: -
Use
whileLoops for Repeated Actions:Repeat the previous exercise but this time using a
whileloop. 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
forLoops to Iterate Over Sequences:- Refactor your main gameplay loop to use a
forloop to iterate over a predefined number of challenges.
- Refactor your main gameplay loop to use a
-
Use
whileLoops for Repeated Actions:- Modify your main gameplay loop to use a
whileloop to continue the game until the environmental health drops below a certain threshold.
- Modify your main gameplay loop to use a