3-1-3-27. Solution: Break, Continue
Quiz Solution: Break the String Here’s one way you could do this. Output:
Build logic into your code with control flow tools! Learn about conditional statements, repeating code with loops and useful built-in functions, and list comprehensions.
Quiz Solution: Break the String Here’s one way you could do this. Output:
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
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
Solution: Count By Output: Another Example Output: If we used the same code above, except with these starting values: This would output: Quiz: Count By Check Output: Another Example Output: If we used the same code above, except with these starting values: This would output: Solution: Nearest Square Output:
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
Solution: Factorials with While Loops Here is our solution for this one: Solution: Factorials with For Loops Here is our solution for this one, using a for loop to find the factorial of a number:
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
“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
Solution: Fruit Basket – Tasks 1 & 2 Output: Solution: Fruit Basket – Task 3 Output:
Quiz: Fruit Basket – Task 1 You would like to count the number of fruits in your basket. In order to do this, you have the following dictionary and list of fruits. Use the dictionary and list to count the total number of fruits, but you do not want to count the other items in … Read more
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
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
Quiz Solution: Create Usernames Output: Quiz Solution: Modify Usernames with Range Output: Quiz Solution: Tag Counter You can use string indexing to find out if each token begins and ends with angle brackets. Output: Quiz Solution: Create an HTML List Output: 다음
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
Solution: Quick Brown Fox Here is an efficient two lines of code, with the desired output. You could of course give your variable any name, it doesn’t need to be word. But word makes sense here! Output: Solution: Multiples of 5 Here is our solution for this one: Output:
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:
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
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
질문 2의 1 트랙 세 개의 변수, 즉 항공 교통 제어 프로그램 상상 altitude, speed그리고 propulsion특정한 항공기에 대한 값은 아래에 지정된있다. 다음 부울 표현식 각각에 대해 True 또는 False로 평가되는지 확인하고 올바른 값과 일치시킵니다. 퀴즈: 객체의 진리값 사용하기 아래 코드는 이전에 본 Who Prize 퀴즈에 대한 솔루션 입니다. 진리 값에 대해 배운 것을 기반으로 이것을 다시 작성할 것입니다. prize당첨된 경우 새 변수 를 사용하여 상품 이름을 … Read more
댓글을 달려면 로그인해야 합니다.