3-1-3-26. Quiz: Break, Continue

Quiz: Break the String Write a loop with a break statement to create a string, news_ticker, that is exactly 140 characters long. You should create the news ticker by adding headlines from the headlines list, inserting a space in between each headline. If necessary, truncate the last headline in the middle so that news_ticker is exactly 140 characters long. Remember that break works in … Read more

3-1-3-25. Break, Continue

For loops iterate over every element in a sequence, and while loops iterate until they’re stopping condition is met. This is sufficient for most purposes but we sometimes need more precise control over when a loop should end. In these cases, we use the break keyword. A loop will terminate immediately if it encounters a … Read more

3-1-3-23. Quiz: While Loops

Quiz: Count By Suppose you want to count from some number start_num by another number count_by until you hit a final number end_num. Use break_num as the variable that you’ll change each time through the loop. For simplicity, assume that end_num is always larger than start_num and count_by is always positive. Before the loop, what do you want to set break_num equal to? How do you want to change break_num each time … Read more

3-1-3-21. Practice: While Loops

If you already have programming experience, feel free to skip any exercises that you don’t feel are necessary for you. Practice: Factorials with While Loops Find the factorial of a number using a while loop. A factorial of a whole number is that number multiplied by every whole number between itself and 1. For example, 6 factorial (written “6!”) … Read more

3-1-3-20. While Loops

“For loops” are an example of definite iteration, meaning that the loop’s body is run a predefined number of times. A “for loop” over a list, executes the body once for each element in the list. A “for loop” using the range function will execute the number of times specified by the range function. This … Read more

3-1-3-17. Iterating Through Dictionaries with For Loops

Iterating Through Dictionaries with For Loops When you iterate through a dictionary using a for loop, doing it the normal way (for n in some_dict) will only give you access to the keys in the dictionary – which is what you’d want in some situations. In other cases, you’d want to iterate through both the keys and values in the dictionary. Let’s see how this … Read more

3-1-3-16. Building Dictionaries

Building Dictionaries By now you are familiar with two important concepts: 1) counting with for loops and 2) the dictionary get method. These two can actually be combined to create a useful counter dictionary, something you will likely come across again. For example, we can create a dictionary, word_counter, that keeps track of the total count of each word in … Read more

3-1-3-13. Quiz: For Loops

Quiz: Create Usernames Write a for loop that iterates over the names list to create a usernames list. To create a username for each name, make everything lowercase and replace spaces with underscores. Running your for loop over the list: names = [“Joey Tribbiani”, “Monica Geller”, “Chandler Bing”, “Phoebe Buffay”] should create the list: usernames = [“joey_tribbiani”, “monica_geller”, “chandler_bing”, “phoebe_buffay”] HINT: Use the .replace() method to … Read more

3-1-3-11. Practice: For Loops

Practice: Quick Brown Fox Use a for loop to take a list and print each element of the list in its own line. Example: Output: Practice: Multiples of 5 Write a for loop below that will print out every whole number that is a multiple of 5 and less than or equal to 30. This should output:

3-1-3-10. For Loops

Now that you’ve learned about conditional statements, let’s move on to loops, which allow us to repeat blocks of code. Python has two kinds, for loops and while loops. Let’s first take a look at the for loop, which we can use to iterate over an iterable. An iterable is an object that can return … Read more

3-1-3-9. Solution: Boolean Expressions for Conditions

Quiz Solution: Evaluate composed boolean expressions altitude < 1000 is False, so we don’t even need to check the second condition – the whole expression is False. propulsion == “Jet” is False, and propulsion == “Turboprop” is False, so the whole expression inside the parentheses is False. It is combined with the other expressions with and, so we don’t even need to check these – … Read more

3-1-3-8. Quiz: Boolean Expressions for Conditions

질문 2의 1 트랙 세 개의 변수, 즉 항공 교통 제어 프로그램 상상 altitude, speed그리고 propulsion특정한 항공기에 대한 값은 아래에 지정된있다. 다음 부울 표현식 각각에 대해 True 또는 False로 평가되는지 확인하고 올바른 값과 일치시킵니다. 퀴즈: 객체의 진리값 사용하기 아래 코드는 이전에 본 Who Prize 퀴즈에 대한 솔루션 입니다. 진리 값에 대해 배운 것을 기반으로 이것을 다시 작성할 것입니다. prize당첨된 경우 새 변수 를 사용하여 상품 이름을 … Read more