6 lessons 32 practices
Iterations and Loops in Python
Saddle up for a thrilling ride through Python’s looping mechanisms! This course is ingeniously crafted to make you loop literate. By the end of this adventure, you’ll be spinning through data with for and while loops, and streamlining code with Pythonic iteration patterns.
Lesson 1: The Interstellar For Loop Journey: Traversing Collections With Ease in Python
Introduction to The For Loop Journey
Welcome! In programming, just like playing a favorite song on repeat, loops execute code repeatedly. Here, we’ll explore the “For Loop” in Python, an iteration construct over sequences such as lists or strings.
Imagine a train journey: the train represents our loop, stopping at each station. Each station represents an item on its route, which is the iterable.
欢迎!在编程中,就像重复播放最喜欢的歌曲一样,循环重复执行代码。在这里,我们将探索Python中的“For Loop”,这是对列表或字符串等序列的迭代构造。
想象一次火车旅行:火车代表我们的循环,停在每个车站。每个车站代表其路线上的一个项目,即可迭代的。
Understanding the Concept of Loops
Like replaying a song or game level, a loop continually executes a block of code until a defined condition is met. It’s akin to saying, “Keep the popcorn machine running as long as the popcorn keeps popping!”
就像重播歌曲或游戏关卡一样,循环不断地执行代码块,直到满足定义的条件。这就像说:“只要爆米花一直在爆炸,就保持爆米花机运行!”
Introduction to For Loops in Python
A Python For Loop looks like this:
| |
In this construct, for and in are keywords. The variable holds the current item in each iteration, while iterable_object can be a list, string, or any object that provides an item sequentially.
Let’s print all elements of a list:
| |
This code will print every planet from the list (Mercury, Venus, Earth, …), each on a separate line.
Riding through Python For Loops: Lists and Sets
Let’s delve further into For Loops by printing each number from a list:
| |
The same works for sets:
| |
Note that because sets are unordered, results might appear in any order.
Riding through Python For Loops: Strings
Strings in Python are also iterable, meaning we can iterate over each character:
Python中的字符串也是可迭代的,这意味着我们可以迭代每个字符:
| |
Riding through Python For Loops: Dictionaries
Finally, you can also iterate over dictionaries, traversing all its keys: 最后,您也可以遍历字典,遍历其所有键:
| |
Output:
| |
| |
「practice」Revealing the Years of Notable Space Missions
Astronaut, we’ve received a transmission that has decrypted the years of key space missions! Look! We have a list, mission_years.
I created a code that uses our trusted Python skills with a For Loop to print out each year. Are you ready for the revelation? Click Run to see them appear!
宇航员,我们接收到了一个解密了关键太空任务年份的传输!看!我们有一份清单,任务年份。 我创建了一个代码,它使用我们信赖的Python技能,通过一个for循环来打印出每一年。你准备好迎接这个启示了吗?点击运行,就可以看到它们出现!(kimi翻译)
| |
「practice」Star Coordinates Calculation Fix
Let’s continue with our interstellar exploration. It seems that a minor bug has surfaced in our calculation of star coordinates. At the moment, the total output of the stars’ coordinates is incorrect. Could you identify and rectify this issue?
Onward to glory and beyond!
| |
The issue in your Python code arises from the way the sum of the coordinates is being calculated within the loop. Currently, the loop is replacing the value of
sum_coordswith eachcoordin the liststars, rather than adding eachcoordto the existing total ofsum_coords. This results insum_coordscontaining only the last coordinate value from the list, rather than the sum of all coordinates.Here’s how you can fix the code to correctly calculate the sum of the star coordinates:
1 2 3 4 5 6 7 8 9 10 11 12# List representing star coordinates in space stars = [1, 2, 3, 4, 5] # Initialize sum sum_coords = 0 # Add each star coordinate to sum_coords for coord in stars: sum_coords += coord # This line is corrected to add each coord to sum_coords # Print sum print("The sum of star coordinates is", sum_coords)With this correction (
sum_coords += coord), each coordinate in the liststarsis added to the totalsum_coords. When you run this corrected code, it will output the correct sum of the coordinates:
1The sum of star coordinates is 15This sum is the result of adding 1 + 2 + 3 + 4 + 5. This should resolve the issue and correctly calculate the total output of the stars’ coordinates.
「practice」Space Race: Finding the Fastest Spaceship
Bingo, Space Adventurer! Let’s make a small detour. In the provided code snippet, your task is to find the top speed from our list of spaceship speeds. You are required to replace the placeholder and complete the loop.
Remember, we have many more galaxies to explore!
| |
| |
Here’s the completed version of your code snippet with the loop implemented to find the fastest speed from the list of spaceship speeds:
1 2 3 4 5 6 7 8 9 10 11 12 13 14# List of spaceship speeds in km/s speeds = {28, 50, 120, 75, 98} # Initialize variable fastest_speed = 0 # Loop through each speed in the set for speed in speeds: # If the current speed is faster than the 'fastest_speed', update it if speed > fastest_speed: fastest_speed = speed # Print the fastest speed print("The fastest spaceship speed is", fastest_speed, "km/s.")This code correctly iterates over the set of speeds, compares each speed with the current
fastest_speed, and updatesfastest_speedif a faster speed is found. Finally, it prints the fastest speed from the list.
「practice」Unveiling the List of Earth-Like Planets
Excellent work, Space Explorer!
Let’s give it another go! There are some space suit models out there.
Can you traverse the suits dictionary and calculate the total weight of all suits there? Give it a try!
| |
| |
To calculate the total weight of all space suits in the dictionary
space_suit_weights, you need to iterate over the values of the dictionary and sum them up. Here’s how you can modify the code snippet to achieve this:要计算字典space_suit_weights中所有太空服的总重量,您需要遍历字典的值并将它们相加。以下是如何修改代码片段以实现此目的:
1 2 3 4 5 6 7 8 9# Dictionary of space suits and their weights in kg space_suit_weights = {'Mercury suit': 21, 'Gemini suit': 16, 'Apollo suit': 32, 'Space Shuttle suit': 50} total_weight = 0 # Use a For Loop to calculate the total weight of all space suits for weight in space_suit_weights.values(): total_weight += weight print('Total suits weight:', total_weight)This code correctly sums up the weights of all the suits in the
space_suit_weightsdictionary and prints the total weight.此代码正确求和space_suit_weights字典中所有套装的重量并打印总重量。
「practice」Charting the Orbits of Planets
Take a look at these planets and their orbits. Your next task is to print the name of each planet along with its orbit period in Earth days. Are you ready, Space Explorer?
| |
| |
It looks like there’s a typo in the list of planets in your Python code snippet. The planet ‘Uranus’ is missing an opening quotation mark. Here’s the corrected version:
| |
Now, the list of planets is correctly formatted, and this script should work without syntax errors.
output
| |
| |
| |
The range(5) command generates numbers from 0 to 4.
Now, let’s experiment with a different value for start and a step:
| |
Output:
| |
As you can see, the above code starts at 1 and goes up to 9, but it only prints every second number due to the step of 2.
如您所见,上面的代码从1开始,一直到9,但由于2的步骤,它只打印每秒钟的数字。
Enumerate: Indexing the Elements
Our next stop is galaxy enumerate(). This function serves as our real-time radar when voyaging through a list, as it provides both the index and value of each item. Here’s how:
| |
Output:
| |
It gives both the index (index) and corresponding checkpoint (check_point) in the journey.
Range Function: Use Case
Time to dock range() and enumerate() together on one spaceship! To illustrate their combined use, let’s consider a group of space cadets and their corresponding IDs.
| |
Here, range(len(cadets)) creates indices for the list cadets from 0 to len(cadets) - 1, allowing us to access both cadets and ids.
Conclusion: Mastery Check and Recap
Great work, Space Explorer! You’ve decoded the mysteries of range() and enumerate(), preparing yourself for a robust for loop journey through your Python universe. Solidify your skills with some practice tasks and build confidence in your newly acquired expertise. Happy coding!
干得好,太空探险家!你已经破译了range()和enumerate()的奥秘,为穿越Python宇宙的强大for循环之旅做好了准备。通过一些练习任务巩固你的技能,并对你新获得的专业知识建立信心。快乐编码!
「practice」Exploring Space with Enumerate and Range
Welcome back, Space Explorer!
Now that we’ve finished learning about the enumerate() and range() functions let’s apply that knowledge to a practical scenario. We possess a list of space objects that we intend to iterate over in two distinct ways: using the enumerate() and range() functions.
But don’t worry, I’ve already made all the necessary arrangements for you. Your task is simply to click Run and observe.
| |
Output
| |
You’re diving into a hands-on exploration of Python’s
enumerate()andrange()functions by applying them to iterate over a list of space objects like the Sun, Earth, Moon, Mars, and Jupiter. Your setup aims to demonstrate two approaches: the direct association of indices and objects usingenumerate(), and the manual control of index order usingrange()for reverse iteration.The code you’ve presented provides an excellent practical demonstration of these concepts:
Using
enumerate(): This part of your code uses theenumerate()function to loop throughspace_objects. It automatically provides each item with its corresponding index, enhancing readability and reducing the risk of off-by-one errors which are common in manual index handling.Using
range(): This section leverages therange()function to iterate over the indices ofspace_objectsin reverse order. It starts from the last element (indexlen(space_objects) - 1) and decrements the index until it reaches 0 (the step is-1). This method is particularly useful when you need to traverse a list backwards, but want to maintain access to the indices for operations like reordering or specific access patterns.By running the provided code, you’ll see a straightforward output showing the index and the associated space object for each position, first in natural order and then in reverse. This kind of iteration can be crucial for tasks that require specific order processing, such as reversing data, creating stacks, or simply when the logic depends on the position of the elements within a list. 你正在深入实践探索Python的enumerate()和range()函数,通过将它们应用于遍历太阳、地球、月亮、火星和木星等空间对象的列表。你的设置旨在展示两种方法:使用enumerate()直接关联索引和对象,以及使用range()手动控制索引顺序以进行反向迭代。
你提供的代码为这些概念提供了一个很好的实践演示:
- 使用enumerate():这部分代码使用enumerate()函数遍历space_objects。它自动为每个项目提供相应的索引,增强了可读性,并减少了手动索引处理中常见的偏移误差。
- 使用range():这一节利用range()函数反向迭代space_objects的索引。它从最后一个元素(索引len(space_objects) - 1)开始,并递减索引直到达到0(步长是-1)。当你需要反向遍历列表,但希望保持对索引的访问以进行排序或特定访问模式的操作时,这种方法特别有用。 通过运行提供的代码,你将看到一个简单的输出,显示每个位置的索引和相关联的空间对象,首先是自然顺序,然后是反向。这种迭代对于需要特定顺序处理的任务至关重要,例如反转数据、创建栈,或者简单地当逻辑依赖于列表中元素的位置时。
Your solution is spot on! Stellar job applying your knowledge of enumerate() and range() functions.
Starred Blocks:
Starred blocks are used to unpack collections into individual variables. They can be used with the enumerate() and range() functions.
enumerate() function:
The enumerate() function takes an iterable (such as a list, tuple, or string) and returns an object that provides both the index and the value of each element. This can be useful for iterating over a collection and keeping track of the current index.
| |
Output:
| |
range() function:
The range() function generates a sequence of numbers within a specified range. It can be used to create a loop with a fixed number of iterations.
| |
Output:
| |
You can also specify a starting and ending point for the range:
| |
Output:
| |
Example using both enumerate() and range():
| |
Output:
| |
Ready for the next challenge? 🚀 你的解决方案非常准确!在应用 enumerate() 和 range() 函数的知识方面做得很棒。准备好迎接下一个挑战了吗?🚀
「practice」Changing Civilizations to Cosmic Objects
Fantastic! Now, it’s time for a minor adjustment. Instead of traversing the list using the range function, change the code to use enumerate, doing the same thing.
太棒了!现在,是时候进行一个小调整了。不要使用range函数遍历列表,而是将代码改为使用enumerate,做同样的事情。
You can do it! Let’s go!
| |
Output
| |
Good try, but it looks like there’s a mix-up. The task was to use the
enumeratefunction to iterate over the list, but your code still usesrange. Remember, withenumerate, you get both the index and the item itself in the loop. How about giving it another shot? Need a hint?
RaR-GPT
It seems you are trying to refactor your code snippet to use the Python
enumeratefunction for iterating over items in a list, while also accessing their indices. Theenumeratefunction is very useful for this because it automatically provides a counter alongside the value from the iterable (like a list), which simplifies the loop and makes the code more Pythonic compared to usingrangeand indexing. Here’s how you can correctly use theenumeratefunction for your list of cosmic objects:您似乎正在尝试重构代码片段以使用 Pythonenumerate函数来迭代列表中的项目,同时还访问它们的索引。enumerate函数对此非常有用,因为它会自动在可迭代的值(如列表)旁边提供一个计数器,这简化了循环并使代码比使用range更Pythonic b2> 和索引。以下是如何正确使用enumerate函数来获取宇宙天体列表:
| |
This version of the code uses enumerate directly on the list cosmic_objects. Notice that I added the start=1 argument to enumerate so that indexing starts from 1 instead of the default 0. This aligns the displayed index numbers with the way you formatted your output in your original snippet, making it easier to read and understand in contexts where indexing traditionally starts from 1.此版本的代码直接在列表 cosmic_objects 上使用 enumerate 。请注意,我将 start=1 参数添加到 enumerate 中,以便索引从 1 而不是默认的 0 开始。这会将显示的索引号与您在原始代码段中格式化输出的方式对齐,使得在传统上索引从 1 开始的上下文中更容易阅读和理解。
「practice」Planetary Index Error Fix
Greetings, Space Voyager!
The names of planets are stored in our database. However, something seems amiss. When we attempt to list the names alongside their corresponding index, an error appears. Could you assist us in resolving this issue? 问候,太空航行者! 行星的名称存储在我们的数据库中。然而,似乎有些不对劲。当我们尝试列出名称及其对应的索引时,出现了一个错误。您能帮助我们解决这个问题吗?
| |
It looks like you are trying to iterate over the list of planets and print each planet’s name and index position. However, there’s a small mistake in the loop statement which is causing an error.![[8D4474F3-F517-426B-A81D-6BB307C9D03F.jpeg]] You need to use the
enumeratefunction to correctly loop through the list with both index and the corresponding value. Here’s how you can correct and enhance your code snippet to achieve this: 看起来你正尝试遍历行星列表并打印每个行星的名称以及它的索引位置。然而,循环语句中有一个小错误导致了一个错误。你需要使用 enumerate 函数来正确地遍历列表,同时获取索引和相应的值。以下是你可以如何修正并改进你的代码片段以达到这个目的:(kimi免费翻译)
| |
In this corrected version, I’ve used the
enumeratefunction, which is designed to return both the index and the value when iterating over a list. This will resolve the error and successfully output each planet with its corresponding index. 在这次修正的版本中,我使用了enumerate函数,它被设计为在迭代列表时返回索引和值。这将解决错误并成功输出每个行星及其对应的索引。(kimi免费翻译)
「practice」Contemplating the Constellations: Indexing and Iterating Star Names
Excellent work so far, Star Navigator!
Your next mission involves arranging our list of stars in two distinct ways, utilizing the functions we’ve learned. Be attentive! You must complete the sections marked with TODOs.
Iterate through the given list using enumerate in direct order and using range in reverse order.
Good luck, Cosmos Conqueror!
| |
Alright, let’s break down your tasks into clear steps and then provide the Python code to achieve your goals.
Task 1: Enumerate the Stars List with Indices Starting from 1
You need to iterate over the list of stars,
stars = ["Sun", "Polaris", "Betelgeuse", "Sirius", "Vega"], and print each star with its index, where indexing should start from 1 instead of the default 0. Python’senumerate()function starts indexing from 0 by default, but you can adjust the starting index by providing a second argument.Here’s how you can accomplish this:
| |
Task 2: Iterate Over the Stars List in Reverse Order Using Range
Now, you want to iterate over the same list in reverse order and also print each star with its index, which should be counted backwards from the length of the list. You can use the
range()function for this, which allows you to specify the start, stop, and step. To iterate in reverse, you set the start at the last index (length of the list), stop before the index 0, and use a negative step.Here’s the code for this:
| |
Both of these tasks utilize Python’s powerful enumeration and iteration capabilities to manage lists effectively. Each snippet accomplishes a specific arrangement and presentation of the data in your list of celestial names. Enjoy navigating through the cosmos with your Python skills!
| |
output
| |
「practice」Generating and Enumerating the First Letters of Planetary Names
Excellent work, Space Explorer! Now, let’s construct a program to explore our planetary systems even more. Please write a program that generates a list of the initial letters from all the planet names and then utilizes the enumerate function to display each letter accompanied by its index number on the console.
| |
It seems like you’re interested in developing a program that not only lists the initial letters of planet names but also uses Python’s enumerate function to pair these letters with their indices for better visualization and understanding. This approach allows for an engaging way to explore planetary names in a programming context.
Here’s a Python program that will achieve your objectives. The program starts by extracting the first letter from each planet’s name and storing these in a list. Then, it utilizes the
enumeratefunction to iterate through this list, printing both the index and the letter, thus providing a clear, indexed list of the first letters of the planetary names.
| |
| |
In this code:
- We iterate over each planet in the
planetslist and extract the first character using indexing (planet[0]).- These characters are then added to the
first_letterslist.- The
enumeratefunction is used to loop overfirst_letters, providing both the index and the value (letter). Each pair is printed in a formatted string that shows the index followed by the corresponding letter.This will output the index and the first letter of each planet, facilitating a straightforward way to reference them by their position in the list.
Lesson 3: Embarking on the While Loop Journey: Steering Through Python’s Control Flow Mechanics
「practice」Exploration of Earth Using a While Loop
Alright, we’re about to unlock the mystery of While Loops in Python! Such an important mission ahead. Let’s crush it together! 💪
Topic Overview and Actualization
Let’s buckle up for our journey into the fascinating world of While Loops. Visualize piloting a spaceship on an uncharted route, making a pit stop at every interesting planet until you find the one that harbors intelligent life. This adventure encapsulates what While Loops do: they continue running tasks until a specific condition changes. In this lesson, we aim to master the usage of While Loops, understand the concept of ‘indefinite iteration’, and control the loop’s execution effectively.
While Loop Discovery
A while loop allows the code to execute repeatedly based on a specific condition. If the condition remains True, it continues to run, similar to an if statement. Let’s look at a while loop that counts from 1 to 5:
| |
The output of the code is:
| |
Here’s the basic structure of a while loop:
| |
In our example, count <= 5 is the condition, and print(count); count += 1 is the code to be executed. As long as the condition count <= 5 holds True, the loop repeats and eventually prints numbers from 1 to 5, inclusive.
Journey through the While Loop Galaxy
Let’s delve into the intricacies of While Loops:
- Firstly, Python checks if the
whileloop’s condition isTrue. - If the condition is
True, it executes the loop’s code. - Then, it cycles back to the first step.
This continues until the condition becomes False.
Steering the While Loop Spaceship - Control Flow
While writing a while loop, make sure the loop’s condition eventually turns False to avoid infinite loops. An infinite loop could potentially crash your system. Here’s an example:
| |
To prevent such a catastrophe, we often use the break statement. The break statement provides an escape hatch, immediately terminating the loop it’s in. We will cover the break operator more extensively later in this course.
The Universe of Indefinite Iteration
while loops offer indefinite iteration, repeating an unknown number of times until a specific goal is achieved. This real-life example demonstrates it:
| |
| |
In this game, your score starts at 0. Every loop iteration increments your score by 2, until you reach a score of 10, at which point you win the game!
Note that if we check for score == 9, this loop will never print the “You won the game!” string.
Lesson Summary
Excellent work! You have just experienced the magic of While Loops! Be observant when crafting While Loops to avoid the dreaded infinite loops.
Now is the time to put your skills to use in the hands-on exercises. You’ll be crafting your while loops, integrating the lessons we’ve learned together. Remember, practice is key to refining your skills! So, wield your coding wand and take on the exercise. If you get stuck, don’t hesitate to ask for help. Happy coding!
「practice」Adjusting Cruise Distance While Approaching Saturn
Alright, Space Wanderer! Let’s get started on our While Loop space mission! We’re staying around Earth, exploring it year by year until we reach 2030.
Observe how Python repeats the print statement as it thoroughly explores the while loop — the starship of our course!
| |
output
| |
Great job on navigating those while loops! You’ve successfully completed the exploration mission. 🚀 Ready for the next adventure?
「practice」Programming the Mars Landing
Fantastic job, Space Voyager! Let’s shake things up a bit. Why don’t we alter the code to stop the ship when it’s 500 million km away from Saturn? Let’s put this to the test!
| |
It sounds like you’re aiming to modify a loop within a script that controls a space voyage simulation, specifically to halt the ship when it is exactly 500 million kilometers away from Saturn. The task involves altering the loop condition so that the loop ceases once the ship’s distance to Saturn reaches or falls below this threshold.
Here’s how you can adjust your loop to meet this new requirement:
| |
With this alteration, the while loop will repeatedly execute, decrementing the distance by 100 million kilometers each time, until distance_from_saturn exceeds 500 million kilometers. Once it’s equal to or less than 500, the loop terminates, and the program prints a greeting indicating arrival in proximity to Saturn. This effectively simulates stopping the ship at the designated distance from the planet.
「practice」Adding Print Statement to Track Orbits
Oh dear, Star Voyager! There’s a minor hiccup in our Mars mission code. It appears our spaceship perpetually voyages to Mars without ever landing. We entrust you to find and rectify this bug! Good luck!
| |
It seems like the primary issue you’re encountering is that the spaceship continues to travel towards Mars but never executes the landing sequence properly. From your description, it sounds as if the spaceship’s loop is designed to run until it has successfully landed, which is controlled by the landed variable. However, despite reaching the correct distance, the loop might not be terminating correctly.
Let’s examine the logic you’ve shared. You have a loop that increments the distance by 10 million km each cycle and checks if the distance equals 85 million km to determine if the spaceship should land. This logic appears sound for landing exactly when the distance is 85 million km. If there are no other conditions affecting the landed variable or other interruptions in your broader codebase affecting this segment, this specific portion should function as intended.
However, if the spaceship is “perpetually voyaging” despite this logic, there are a few potential issues to consider:
- Infinite Loop: If the loop continues beyond the specified landing condition, it could be due to the loop condition not updating as expected. Make sure
landedis correctly set toTrue. - Condition Miss: If the increment leads the
distanceto skip over exactly 85 (like if distance changes due to other conditions elsewhere in the code), the condition to land won’t be met. This doesn’t seem to be the case here since you’re incrementing by 10, but it’s a common issue in similar scenarios. - External Modifications: Ensure no external modifications to
distanceorlandedfrom other parts of the program that might affect the execution flow here.
Based on the provided code snippet, the program should correctly report landing when the distance reaches exactly 85 million km. If the problem persists, I would recommend checking for any external interferences or modifications to the distance or landed variables outside the shown code block. If the issue is solely within the provided code, then it should function as expected under normal circumstances.
Thank you for your patience! It seems like I need to look closer at the loop’s termination condition based on the hint you provided. Currently, the loop increments the distance by 10 million km each iteration, and the landing is supposed to occur when the distance is exactly 85 million km. However, if there is any inconsistency or missed increment that causes the distance to overshoot 85 million km without hitting it exactly, the landing would indeed not trigger, causing the spaceship to continue indefinitely.
One way to solve this problem would be to adjust the landing condition from an equality check (==) to a greater than or equal to check (>=). This change ensures that even if the distance overshoots 85 million km due to modifications elsewhere or potential rounding errors, the spaceship will still land. Here’s how you could adjust the code:
| |
This adjustment will ensure that the spaceship lands once it reaches or exceeds 85 million km, preventing it from continuing indefinitely if it somehow misses the exact 85 million km mark. This might be particularly important in a more complex program where distance could be influenced by other functions or conditions in ways that might cause unexpected behavior.
| |
「practice」Reverse Engineering the Voyage to Venus
You’ve done a stellar job so far, Space Explorer! Let’s continue our exploration of the universe. This time, you’ll need to write some code on your own. Complete the TODO comment in the code to maintain the orbit of the Earth!
| |
You’re gearing up to track the Earth’s orbit through a loop in Python, and you need to complete the loop and print statements. Here’s a clear way to complete the provided code:
- Condition for the While Loop: The loop should continue as long as
orbit_cyclesis less than 10. - Print Statement: Every iteration (orbit) should print out the current number of orbits completed by Earth.
Here’s the completed code:
| |
This script initializes the number of orbits at 0 and uses a while loop to increment this number until it reaches 10. Each time through the loop, it prints a message stating how many orbits Earth has completed. The output is updated each loop, so it accurately reflects the new number of completed orbits.
output
| |
Time to wrap up our exploration journey, Pilot! Let’s reverse-engineer the journey from Venus to Earth. Please modify the given starter code to have the spacecraft start from Venus and move closer to the Earth by a million km increment until it reaches Earth.
Use TODO comments as a reference. Let’s go!
| |
Here is your modified code, reflecting a journey from Venus to Earth with the spacecraft reducing its distance by 1 million km on each loop iteration until it reaches Earth:
| |
This script will output the distance from Venus to Earth in each iteration of the loop, decreasing the distance by 1 million kilometers each time, and it will stop when the spacecraft has reached Earth.
output
| |
Lesson 4: Interstellar Strolls Through Python: Loop and Conditional Symbiosis
Lesson Introduction: Combining Loops with Conditionals - The Power Duo
Greetings, student! Today, we’re fusing Python loops and conditionals together. Conditionals empower our code to make decisions, while loops enable the execution of repetitive tasks. Let’s master this synergy!
The Basics of Conditions in Loops
Loops, such as for and while, repeat specific tasks, and conditionals — if, elif, and else — guide the path of the code. Combining these constructs equips us with a virtual super robot that performs repeated tasks with decision-making abilities.
Let’s consider sending personalized party invitations. In this context, loops go through each guest, and conditionals decide the style of the invitation:
| |
This code prints:
| |
Working with Conditionals in For Loops
Python’s For Loop iterates over a defined sequence of elements. When we pair a conditional with the loop, the execution adjusts with each iteration based on the condition.
For instance, consider hosting a party. We have a guest_list and an unwanted_list. By pairing a For Loop with a conditional, we can ensure that only welcomed guests gain admission:
| |
The code prints:
| |
Implementing Conditionals in While Loops
A While Loop continues as long as its condition remains valid. Inserting a conditional within it can alter or halt its iterations based on changing conditions.
Suppose that when an unwanted guest arrives, the doorman closes the gate:
| |
The code prints:
| |
It looks like you’ve shared a Python code snippet using a while loop to manage a list of guests at a party, including a condition to check for unwanted guests. The code iterates over the guest list, inviting each one unless they are found in the unwanted guests list. When an unwanted guest is encountered, the loop terminates with a specific message indicating the party is over because of that guest’s arrival.
To clarify and expand upon the explanation: In your script, guest_list contains names of all invited people, while unwanted_guests lists those whose arrival would end the party. You initialize guest_index to 0 to start checking from the first guest in the list. The while loop continues as long as guest_index is less than the number of guests. Inside the loop, there’s a condition to check if the current guest (guest_list[guest_index]) is not in the unwanted_guests list. If true, a welcoming message is printed. If false, a message indicating the end of the party due to the unwanted guest is printed, and the loop is immediately stopped with break. The guest_index is incremented at the end of each loop iteration to move to the next guest.
This mechanism ensures that as long as no unwanted guests show up, everyone on the list is welcomed sequentially. The party continues until an unwanted guest arrives, at which point the script stops checking further guests. If you have any questions about modifying or understanding this script further, feel free to ask!
Understanding Different Scenarios
The combination of loops and conditionals provides immense versatility. For instance, consider these scenarios:
- Picking out even numbers from a list.
- Find all duplicates in the list of numbers.
Here’s how we can address these scenarios:
| |
output
| |
Lesson Summary: The Power Duo in Action
Fantastic! You’ve learned to combine Python’s loops and conditionals. We’ve covered for and while loops coupled with conditionals and showcased Python examples, using our combination to solve various scenarios.
Now, it’s time to exercise this new skill through practice. Just as a dancer perfects a dance routine by repeating each step, mastering these concepts requires ample practice. Let’s continue our journey to Python mastery!
「practice」Welcome the Party Guests
Welcome to our space party planner, Space Explorer! The guest list is quite extensive, but the party room, unlike the TARDIS, has limited space and time. Consequently, I designed a code that utilizes loops and conditionals, and invites VIP and Regular guests until the time_left becomes 0, while avoiding Unwanted guests.
Please press Run to see how it operates.
| |
以上运行🙅❌ guest[1] ,guest[0],# You are hosting a party, and you have guests in line.
You will invite-only VIPs and Regular guests until the time is up!
Your goal is to greet as many people as you can, or else the party might be too quiet.
自动生成
| |
RAR- GPT4
You’re focusing on ensuring that your guest invitation process at your party doesn’t exceed the allotted time, specifically by managing the decrementing of the time_left variable correctly. The challenge here is that you might invite one extra guest because the decrement of time_left happens after a guest is already welcomed, and the check for whether time_left has reached zero isn’t optimally placed to preemptively prevent this.
To address this, you need to adjust the placement and condition of the time_left check. This involves making sure you have enough time left before you invite each guest. Here’s a refactored version of your loop to ensure time_left is checked properly before welcoming any guest, and the loop exits correctly when no more time is left:
python
Copy code
| |
「practice」Adjusting Invitations for VIP Guests Only
Great job, Cosmic Voyager!
Now, let’s fine-tune our party invitation system a bit more.
Alter the code to ensure that a guest receives an invitation only if they are classified as a 'VIP'. Non-VIP guests should receive a decline message, specifying their name and that they can’t enter the party.
You’ve got the drill!
| |
自动生成
| |
output
| |
「practice」Cosmic Party Guest Greetings Issue
Hold your horses, Space Explorer! Something has gone haywire in our greeting system at the Cosmic party. The current code was intended to generate a different greeting based on the length of the guest’s name, but that just isn’t happening.
Can you help us fix the greeting machine’s logic? We need it to provide longer, more formal greetings to guests with names longer than 4 letters and shorter, more casual greetings to guests with names that are 4 letters or shorter.
| |
自动生成
| |
output
| |
「practice」Write Conditions for Party Guest Entry
Great job, Voyager! It’s time to kick things up a notch. I aim to send different messages to my party guests based on the number of star fruits they contribute. Could you assist me in completing the code to make this possible?
TODO comments can guide you through!
| |
自动生成
| |
output
| |
「practice」Preparing Personalized Messages for Space Party Guests
Great job, Cosmic Explorer! There’s just one final task before we move on. How about trying to write the logic for the party guest list from scratch?
We should utilize the for loop and conditionals to send individualized messages to the guests based on their RSVP status.
| |
| |
Lesson 5: Unmasking Nested Loops: Navigating Advanced Looping Structures in Python
Introduction and Overview
Welcome, Python astronauts, to the intergalactic tour of nested loops in Python! Just like spaceships in formation, nested loops tackle layered problems. Our mission today is to understand the syntax and applications of nested loops, all of which will be enriched with a practical example.
Starry Dive into Nested Loops
Nested loops are simply loops within loops. They function much like stars and planets in the cosmos. Each celestial body (an outer loop star) has smaller bodies (inner loop planets) revolving around it. Similarly, for each iteration of an outer loop, an inner loop executes completely.
Syntax and Structure of Nested Loops in Python
Nested loops follow a hierarchical structure. For each iteration of an outer loop, an inner loop executes fully:
| |
Here’s an example of a nested loop using Python’s range() function. In this example, i represents different types of spaceships, and j represents various spaceship features:
| |
The code prints:
| |
Traversing the Cosmos with the While Loop in Python
Nested while loops also use an outer-inner structure:
| |
Here’s an example with nested while loops:
| |
The code prints:
| |
Deeper Dive: Complex Nested Loop Scenarios
Nested loops are not necessarily limited by just two-level nesting. In fact, there can be any number of nested loops. Here is a simple example with three nested loops:
| |
While analyzing three-dimensional data can be more informative, it’s crucial to ensure the computational effort doesn’t exceed the capacity of your hardware. But don’t worry if that doesn’t make too much sense right now, you’ll learn more about it in the next courses!
Lesson Summary
Congratulations, astronaut! You’ve successfully journeyed through nested loops. We’ve navigated the landscape of nested loops, their syntax, and practical, celestial-themed examples. Up next are some practice exercises! Buckle up for a thrilling ride through the nested loops cosmos!
「practice」Spaceships and Planets: Traversing with Nested Loops
Guess what, Space Voyager? We have an array of spaceships heading towards various planetary systems! We will employ the power of nested loops in Python to determine which spaceship is directed to which planetary system.
Everything is programmed; you just need to press the Run key to obtain the details!
| |
| |
「practice」Navigating Through Nested Loops in the Cosmo System
Well done, Space Explorer! Our interstellar neighborhood, Cosmo, is home to three planets. Each planet, in turn, has five orbits. In the provided code, you will journey through each orbit of every planet in Cosmo. Once you press the Run button, the details of your excursion will be displayed. Embark on this exploration through nested loops now!
| |
Title: 03_Python_Iterations and Loops in Python
Starred Blocks:
Well done, Space Explorer! Our interstellar neighborhood, Cosmo, is home to three planets. Each planet, in turn, has five orbits. In the provided code, you will journey through each orbit of every planet in Cosmo. Once you press the Run button, the details of your excursion will be displayed. Embark on this exploration through nested loops now!
| |
Output:
| |
You’ve presented a Python script and a description that illustrates a scenario of traversing through orbits of planets within a fictional system named Cosmo. It seems that you’re using this example to demonstrate how nested loops can simulate traversing through multiple layers of a system, such as planets and their orbits.
Your code snippet is set to iterate over these entities, and you’ve set it up with a couple of variables (cosmo_id, planet_limit, orbit_limit) to control the looping process. Specifically, your outer loop represents the planets (labelled as Cosmo with IDs), and the inner loop represents the orbits around each planet.
However, your description mentions that Cosmo contains three planets, each with five orbits, but your code is set to loop through four planets (planet_limit = 4) and six orbits (orbit_limit = 6). This discrepancy might lead to confusion about the actual structure of Cosmo based on your narrative. To align the code with your description:
- You might want to adjust
planet_limitto4(since counting starts from 1 and it should stop before hitting 4 to encompass three planets). - Similarly, adjust
orbit_limitto6to correctly iterate through five orbits per planet.
Here’s how your code would look if you match it with the narrative description you’ve provided, ensuring that it will accurately reflect the journey through three planets and five orbits per planet:
python
复制代码
| |
This corrected script will now reflect your narrative more closely and provide a clear demonstration of nested loops functioning as intended. Once run, this script will iterate through each of the three planets, exploring all five orbits around each one, exactly as you described in your setup.翻译成中文
此更正后的脚本现在将更准确地反映您的叙述,并清晰地演示了嵌套循环按预期正常运行。一旦运行,该脚本将遍历三个行星中的每一个,探索每个行星周围的全部五个轨道,这与您在设置中的描述完全一致。
「practice」Planet Gazing from Stars: Changing Print Statements
Excellent work, Space Explorer! Now let’s make a change. Instead of having planets witness stars, we’ll switch it around. Change the print statement in the code so it displays which planet is seen from each star. Let’s go!翻译成中文
干得好,太空探险家!现在我们做个更改。不再让 planets 见证 stars,而是反过来。更改代码中的
| |
| |
I can help explain technical concepts, clarify doubts regarding practices, guide you through solving coding challenges without giving away the direct solution, and check your solutions to ensure they’re on the right path. If you’re stuck or need a hint on refining your code, just let me know how I can assist you!
Notice the error message regarding indentation. Python is strict about indentation as it defines the block of code that belongs to a loop or condition. In your code, the print statement inside the loop should be indented. Try adjusting the indentation for the line inside your for loop like this:
| |
This keeps your code structured and solves the indentation error. Give it a try and see if it resolves the issue! output
| |
「practice」Stellar Constellation Corrections
Admirable work, Star-tracker! We’ve just received a report detailing a slight turbulence in the Python code controlling our star-tracking system. The code is designed to log questions about stars and their corresponding constellations; however, there appears to be an issue. Could you help us identify and rectify the problem?
太棒了,Star-tracker!我们刚刚收到了一份报告,详细说明了控制我们_恒星追踪系统_的 Python 代码中出现了一些轻微的紊乱。该代码旨在记录有关恒星及其对应星座的问题;然而,似乎存在一个问题。你能帮助我们找出并纠正这个问题吗?
| |
It seems you’re attempting to create a Python script that checks whether certain stars belong to specified constellations by using nested loops. However, there is a confusion in your loop structure and variable naming that needs to be addressed to make the script functional.
Original Script Problem
- Variable Overlap: In your nested loop, you’re using
stars_in_skyas both the list of stars and as the iterating variable for the inner loop. This causes the liststars_in_skyto be overwritten by the string values ofconstellation, which disrupts the intended logic of iterating over stars.
Corrected Version
You should maintain separate variables for iterating over the list of stars and the list of constellations. Here is a corrected version of your code:
| |
Explanation
- Outer Loop: Iterates through each constellation.
- Inner Loop: Iterates through each star in the
stars_in_skylist without modifying the list itself. - Print Statement: Outputs a question for each combination of star and constellation, which seems to be your original intent.
This correction ensures that each star is checked against each constellation without any overlap in variable names or unintended overwriting of data.
output
| |
「practice」Space Mission Assignment
Great job, Space Voyager! Now, let’s see if you can chart your own course.
Your task is to fill in the code to assign each astronaut to each planet in the mission plan.
| |
To achieve the task of assigning each astronaut to each of the planets, we’ll need to create a nested loop structure within your existing code. Here’s how to think about it: You have a list of astronauts and a list of planets, and you want to pair each astronaut with every planet. This means for each astronaut, you will loop through all the planets and print a statement that assigns them to a planet.
Here’s the detailed approach and the completed code:
- Outer Loop - This loop will go through each astronaut in the list
astronauts. - Inner Loop - Inside the outer loop, this loop will iterate over each planet in the list
planets. - Print Statement - Within the inner loop, you’ll print a statement that assigns the current astronaut from the outer loop to the current planet from the inner loop.
Here is how you can fill in the code:
| |
This code will result in each astronaut being printed alongside each planet, effectively simulating the scenario where each astronaut is tasked with visiting each of the three planets. output
| |
「practice」Adding Orbits to Our Universe
Great work, Stellar Navigator!
Next, you will enhance your universe. For each planet, you will illustrate its orbits. You need to implement the innermost loop specifically for the orbits. Using the knowledge you’ve acquired thus far, can you figure that out?
| |
output
Cosmos: 1
Planet: 1
Planet: 2
Planet: 3
Cosmos: 2
Planet: 1
Planet: 2
Planet: 3
Cosmos: 3
Planet: 1
Planet: 2
Planet: 3
| |
You’re seeking to enhance a Python script to simulate a universe with multiple cosmos, planets, and orbits. In the existing script, you have already set up the framework for cosmos and planets, and now you want to expand this structure to include orbits for each planet. To do this, you will need to add another nested loop that iterates through each orbit of a planet, printing out the orbit number for every planet in each cosmos.
Here’s an expanded and modified version of your script that includes the implementation of orbits for each planet:
| |
This script now includes a third loop nested within the planet loop, which handles the orbits. For each planet, it iterates three times, representing three orbits. This structure allows you to simulate a universe where each of three cosmos contains three planets, and each planet has three orbits. The indentation increases with each level to visually separate the different levels of the simulation, making it easier to understand the hierarchy and flow of cosmos, planets, and orbits.
Great! Let’s break down the task and work on implementing the orbit loop step-by-step within the structure of your existing code.
Your task is to create an innermost loop that handles the orbits for each planet. Here’s the skeleton code with annotations explaining each part:
- Initialize the orbit variable: Just like you initialize the
planetvariable before starting its loop, you’ll need to initialize theorbitvariable before you start the orbit loop. - Start the orbit loop: This loop should iterate three times since each planet has three orbits.
- Set a condition for the loop: This will ensure that the loop iterates the correct number of times (three in this case).
- Print the orbit inside the loop: This is where you’ll show the number of each orbit.
- Increment your orbit counter: This is crucial to avoid creating an infinite loop and ensure the loop progresses to the next iteration.
Here is how you can integrate these steps into your existing code:
| |
This structure makes sure that for each planet in each cosmos, three orbits are printed, demonstrating the nested looping technique in Python. With this setup, each while loop handles a specific level of the hierarchy: cosmos, planets, and orbits.
Would you like to run this code to see how it outputs, or do you need further explanations on any of these parts?
debug
It appears there isn’t any code in your current submission. This could happen if you accidentally deleted your code or didn’t save your changes before running your code.
Let’s try this step by step again:
- Make sure you’re starting with the initial snippet provided in the practice.
- Then, add the inner loop for orbits as discussed before.
Here’s a refresher on the structure to help guide you:
| |
Please make sure to carefully copy your code, including the orbit loop, into the IDE. After you’ve added your code, remember to hit “Save” or ensure it’s saved, then run it again. If you face any specific challenges or errors, let me know, and I’ll be more than happy to help you debug!
output
| |
「practice」Orbiting through Cosmos: Nested Loops in Action
Well done, Space Explorer! Now, it’s your turn to implement the same concept from scratch! In the cosmos, each planet has a varying number of satellites.
Your mission is to write a program that uses nested loops to traverse each planet, along with each respective satellite.
| |
翻译
| |
| |
当然可以解释。这段代码的目标是遍历每个行星及其相应的卫星。
for planet, num_satellites in satellites.items():这个循环通过遍历satellites字典的项 (items) 来进行。每一次迭代,它都会提取键值对planet(行星名字)和num_satellites(相应的卫星数量)。对于字典中的每个行星(
planet),内嵌循环for satellite_order in range(1, num_satellites + 1):将会执行。这个内部循环用于遍历从1到num_satellites(行星的卫星数量)的每一个数字,表示卫星的顺序。print(f"Satellite {satellite_order} around {planet}")这一行会在内部循环中为每个卫星打印其顺序和它所围绕的行星。f在print函数前面表示这是一个格式化字符串,允许你将变量直接插入字符串中。所以每次循环时,{satellite_order}会被替换成当前的卫星顺序,{planet}会被替换成当前行星的名字。
简单来说,这段代码首先遍历所有行星,并且对于每个行星,它会遍历其所有卫星,并为每个卫星打印一个包含其顺序和所属行星名字的消息。
output
| |
Lesson 6: Commanding Loops: Mastery of Break and Continue in Python
Setting the Stage: Control Over Loops with Break and Continue
Hello, and welcome to this stimulating session! Today, you will delve into Python loops’ governing principles with break and continue. These potent tools can halt a loop mid-way or bypass an iteration.
Sounds thrilling? Let’s dive in!
Break: The Loop Controller in For Loops
The break keyword ends a loop before it exhausts all iterations:
| |
The code prints:
| |
In this for loop, once we reach Earth, break terminates the loop. We avoid unnecessary iterations over the remaining planets.
Break: The Loop Controller in While Loops
The break command works similarly in a while loop:
| |
The code prints:
| |
Continue: The Loop Skipper
The continue keyword omits a part of the current loop iteration and proceeds to the next:
| |
The code prints:
| |
After encountering Mars, continue skips the printing command and jumps to the next planet.
Nested Loops and Loop Control
break and continue also operate within nested loops. In them, break only stops the innermost loop it operates in:
| |
The code prints:
| |
Lesson Summary
Give yourself a pat on the back; you’ve just overcome a significant hurdle in your Python learning journey! You’ve deciphered how to control loops using break and continue. You have understood their roles in single and nested loops. Upcoming hands-on exercises will further refine these concepts. Brace yourselves, and let’s dive in!
「practice」Observing Celestial Bodies with Safety Measures
「practice」Preserving Telescope Battery Power in Space
Woof-woof! Excellent job, Space Explorer! In the starter code, we are observing the visibility of some famous constellations. Your job is to skip the Orion visibility processing, thus saving some battery power on our telescope.
Ready? Let’s go!
| |
| |
「practice」Fixing the Visibility Check in the Astronomy Observation Code
To skip the visibility processing for Orion and save some battery power, you need to insert a condition to check when the name is “Orion” and then use continue to skip the rest of the current iteration in the loop. Here’s a hint to get you started on modifying the loop:
Before printing the constellations, check if the name is “Orion”. If it is, you can skip the current iteration without printing its visibility statuses.
Can you think of a way to implement this using a conditional statement and the continue keyword you learned about in the lesson?
| |
| |
| |
「practice」Adding a Break Condition to Conserve Energy
Nicely done, Star-gazer! It seems there’s a small hiccup in our next data observing session.
Try running the provided code and identify the issue that prevents us from correctly traversing astronomical objects based on their visibility statuses - when we detect the invisible object, we should leave it immediately! Are you able to correct it?
| |
| |
| |
这段代码的目的是处理一个包含天文对象数据的列表。每个天文对象有两个属性:类型(如星星、行星等)和可见性状态的列表(可见或不可见)。这个程序会遍历每个天文对象,然后再遍历它们的可见性状态。
首先,我们定义了一个名为
astronomy_objects_data的列表,其中包含了不同天文对象的数据。每个对象都是一个包含两个元素的列表:天文对象的类型(object_type)和一个表示可见性状态的列表(visibility)。使用第一个
for循环,我们遍历astronomy_objects_data列表中的每个天文对象。在每次迭代中,我们通过解构来获取天文对象的类型和它的可见性状态列表。接着,我们打印出正在探索的天文对象类型。
然后,使用第二个
for循环,我们遍历当前天文对象的每个可见性状态。在每次迭代中,我们检查状态是否为invisible(不可见)。如果状态为
invisible,我们打印出“Invisible object detected, we should leave the object immediately!”(检测到不可见对象,我们应立即离开此对象!),然后使用break语句立即退出当前正在遍历的可见性状态列表的循环。这意味着如果我们遇到一个不可见的对象,我们将不会继续检查该天文对象的其他可见性状态。如果状态不是
invisible(即可见),我们打印出可见性状态。
使用 break 替换原代码中的 continue 改变了程序的行为。continue 会跳过当前迭代的其余部分并继续下一个迭代,而 break 会完全结束循环。这样,当遇到不可见对象时,我们不会继续检查同一天文对象的其余状态,符合题目的要求。
break和continue是Python中控制循环流程的两个关键字,它们的用法和作用有明显的区别:
break用于完全结束当前循环,无论是for循环还是while循环。当break被执行时,循环将停止迭代,且退出循环体,继续执行循环后面的代码。continue用于跳过当前循环的剩余代码,并直接进入下一次迭代。它只影响当前迭代的执行,循环本身不会被终止,而是继续从下一次迭代开始执行。
简单地说,break 是“立即停止循环”,而 continue 是“跳过当前迭代”。
当然,我来通过一个简化的例子,详细解释 break 和 continue 的区别。
假设我们有一组数字,并且我们想对它们进行一些检查。
示例:数字列表的遍历
列表:
| |
目标:
- 当我们遇到数字
5时,打印一条消息并完全停止遍历(使用break)。 - 当我们遇到数字
3时,仅跳过这个数字并继续遍历其他数字(使用continue)。
使用 continue:
| |
输出:
| |
在这里,continue 让我们跳过了数字 3,但没有停止整个循环。
使用 break:
| |
输出:
| |
在这里,break 让我们在数字 5 出现的时候立即停止遍历列表,即使列表中还有其他数字。
这个例子展现了 continue 用于跳过某些条件下的迭代,但允许循环继续;而 break 用于在满足某个条件时完全终止循环。
「practice」Navigating Celestial Bodies: Observations in Space
Splendid work, Cosmic Coder! Now, let’s see if we can take a closer look at these galaxies. Could you modify the code to break the loop when we find the first 'invisible' galaxy? Doing so would help us conserve energy.
| |
| |
输出
| |
Alright, Space Galactic Pioneer! Your final challenge awaits! You have everything in place; it’s time to fetch some data and iterate over it using loops.
Remember to print the name of each celestial body. However, if a body is unobserved, print the warning message and proceed to the next one. Just like in real-life data processing, not every piece of data is useful or safe.
Let’s get started!
| |
| |
让我们一步步解释代码的含义:
定义星体数组
celestial_bodies = [('Star 1', "observed"), ('Star 2', "unobserved"), ('Star 3', "observed"), ('Star 4', "unobserved")]
这里定义了一个名为 celestial_bodies 的数组,包含四个星体,每个星体是一个 tuple,包含星体的名称和观测状态(observed 或 unobserved)。
遍历星体数组
for body, status in celestial_bodies:
这里使用了一个 for 循环来遍历 celestial_bodies 数组。每次循环都会将当前星体的名称和观测状态赋值给 body 和 status 变量。
观测星体
print(f"Observing {body}...")
在每次循环中,首先打印出当前星体的名称,表示我们正在观测这个星体。
检查星体是否未观测
if status == "unobserved":
这里检查当前星体的观测状态是否为 “unobserved”。
警告未观测星体
print(f"Warning: {body} is unobserved. Skipping...")
如果星体未观测,我们打印出一个警告信息,表示我们将跳过这个星体。
continue
使用 continue 语句跳过当前循环,继续下一个星体。
打印星体信息
print(f"{body} is {status}.")
如果星体已经观测,我们打印出星体的名称和观测状态。
print()
最后,我们打印出一个空行,以便与下一个星体的信息分开。
总的来说,这个代码遍历了星体数组,对每个星体进行观测,如果星体未观测,就打印出警告信息并跳过;否则,打印出星体的名称和观测状态。
Looking at your code, I see you’re on the right track but a little adjustment is needed. Let’s focus on two main areas:
Where you’ve placed the
continuestatement: Currently, thecontinueis outside theifblock and will always be executed, which isn’t the intention. You want to only skip the rest of the loop’s body when a celestial body is “unobserved”.Printing the observed status: The final
printstatement for displaying the body’s observed status is outside the loop. It should be part of the loop but only executed if the celestial body is not skipped.
Here’s a hint:
- Ensure the
continuestatement is part of the condition that checks if the celestial body is unobserved. This will correctly skip the unobserved bodies. - For bodies that are not being skipped, ensure your next
printstatement about the celestial body being observed is within the loop but after your condition checking for “unobserved” status. This way, it only prints this information for bodies that do not meet the “unobserved” condition.
Try adjusting your code with these hints, and remember to closely check your loop and conditional block syntax to make sure each action happens where and when you intend it to.
| |
输出
| |