For loops: Code tracing For each of the following questions, predict the code output, and write...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
For loops: Code tracing For each of the following questions, predict the code output, and write the answer into the worksheet (and if you are not sure, check your answer by typing in the code into the Python Interpreter - but trace your code on paper first): (1) Given the code for counter in range(2): print(counter) what will the output be? Answer: (2) Given the code var = "TO" lenVar = len(var) for val in lenVar: print(val) Any issues with the above code - will it run? Explain: (3) Given the code: var = "TO" lenVar= len(var) for val in var: print(val) Compare the code to the one in question 2 above. What is the difference? What will the output of (3) be? Answer: (4) Given the code Tutorial 5 var = "snow" for pos in range( len(var)): print(pos, var[pos] ) what will the output be? Answer: (5) Given the code str1 = input("Enter a name without the letter e: ") for i in range( len(str1) ): if str1[i] "e": print("Found an e! ") break print(str1[i]) print("after loop "D If we run the above code and type in "ben", what is the output? Answer: If we run the above code again and type in anna", what is the output? Answer: (6) Given the code str1 str1_len = res = for ch in str1: Hints: = "a longer string" len(str1) print(res) What is the output? Answer: 11 11 res = res + Coding exercises 1. [Files] Write a program that asks the user for a name of a file (it can be anything - but make it something easy to remember like totals). Once that is provided, the program outputs a series of numbers to the file starting from 0 up to but not including 20 (0, 1, 2, 3, ... 19) one number per line. Use a for loop to do this. 11 11 + ch To write to a file, you have to open that file with mode "w" as a second parameter. See slides/book if you don't remember what this means. You will need to add a newline to each line you write to the file, which is " " See the slides for syntax details Copy your code here or upload your python file to Brightspace. As stated above, use a for loop - the sequence is provided by the range function the function that writes to a file must be given a string argument. 2. [Files] Write a second program in a new window that asks the user for the name of a file they MUST input the name that you gave the file in part (1). After they input the file name, the program reads the specified file line by line, and when it's done reading the file, outputs the sum of the numbers to the python shell (i.e., not to the file, so just use a regular print statement). - Hints: the file name you give your program is the one you gave in question 1 above, spelled exactly the same. The file must be in the same location as the python program. Use a for loop Use a variable to accumulate the sum Copy your code here or upload your python file to Brightspace 3. [Strings] This question does not involve files. Write a program that uses a single input statement to ask the user to enter their first and last name (all in one line, where first and last name must be separated by a blank. The program will grab the first name and store it in one variable, and grab the last name and store it in a second variable, and then print these on two lines. Example: Enter your first and last name on one line: kasia muldner kasia muldner Copy your code here or upload your python file to Brightspace. For loops: Code tracing For each of the following questions, predict the code output, and write the answer into the worksheet (and if you are not sure, check your answer by typing in the code into the Python Interpreter - but trace your code on paper first): (1) Given the code for counter in range(2): print(counter) what will the output be? Answer: (2) Given the code var = "TO" lenVar = len(var) for val in lenVar: print(val) Any issues with the above code - will it run? Explain: (3) Given the code: var = "TO" lenVar= len(var) for val in var: print(val) Compare the code to the one in question 2 above. What is the difference? What will the output of (3) be? Answer: (4) Given the code Tutorial 5 var = "snow" for pos in range( len(var)): print(pos, var[pos] ) what will the output be? Answer: (5) Given the code str1 = input("Enter a name without the letter e: ") for i in range( len(str1) ): if str1[i] "e": print("Found an e! ") break print(str1[i]) print("after loop "D If we run the above code and type in "ben", what is the output? Answer: If we run the above code again and type in anna", what is the output? Answer: (6) Given the code str1 str1_len = res = for ch in str1: Hints: = "a longer string" len(str1) print(res) What is the output? Answer: 11 11 res = res + Coding exercises 1. [Files] Write a program that asks the user for a name of a file (it can be anything - but make it something easy to remember like totals). Once that is provided, the program outputs a series of numbers to the file starting from 0 up to but not including 20 (0, 1, 2, 3, ... 19) one number per line. Use a for loop to do this. 11 11 + ch To write to a file, you have to open that file with mode "w" as a second parameter. See slides/book if you don't remember what this means. You will need to add a newline to each line you write to the file, which is " " See the slides for syntax details Copy your code here or upload your python file to Brightspace. As stated above, use a for loop - the sequence is provided by the range function the function that writes to a file must be given a string argument. 2. [Files] Write a second program in a new window that asks the user for the name of a file they MUST input the name that you gave the file in part (1). After they input the file name, the program reads the specified file line by line, and when it's done reading the file, outputs the sum of the numbers to the python shell (i.e., not to the file, so just use a regular print statement). - Hints: the file name you give your program is the one you gave in question 1 above, spelled exactly the same. The file must be in the same location as the python program. Use a for loop Use a variable to accumulate the sum Copy your code here or upload your python file to Brightspace 3. [Strings] This question does not involve files. Write a program that uses a single input statement to ask the user to enter their first and last name (all in one line, where first and last name must be separated by a blank. The program will grab the first name and store it in one variable, and grab the last name and store it in a second variable, and then print these on two lines. Example: Enter your first and last name on one line: kasia muldner kasia muldner Copy your code here or upload your python file to Brightspace.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
What aspects of control must exist before a subsidiary is consolidated?
-
Hamed Hazara Advertising has always used the direct write-off method to account for uncollectibles. The company's revenues, bad-debt write-offs, and year-end receivables for the most recent year...
-
Fill in the blanks to make the following statements correct. a. Suppose the Canadian government announced that all homeowners must insulate their homes with R-40 insulation. As a pollution-control...
-
Who is currently in your own network that you could use for prospecting? How might you add to your network?
-
The COSO internal control framework identifies five components of an entity's internal control. Which, if any, of those five components of Avon's internal control were flawed? Explain. If there were...
-
OX | ChatG ChatG U RealEs New ta Inbox M Inbox Week a single Q single P Pea $449?module_item_id=828652 A G VERABLES Chapter 7 Question 7-28 The following are examples of audit procedures: 1. Watch...
-
An asset with its purchasing price of Rs. 530,000 was imported from its manufacturing place holding Rs. 80,000 as carrying cost. Installation charges for that asset are Rs, 27,500. Tax paid to the...
-
Describe the features of a line of credit.
-
What is a corporate bond? A Treasury bond? A municipal bond?
-
What is accumulated depreciation, and how does it tie into the income statement?
-
Consider a baseball that is caught and thrown at the same speed. Which case illustrates the greatest change in momentumthe baseball (a) being caught, (b) being thrown, or (c) being caught and then...
-
Why has finance historically been an early adopter of new technology?
-
The DJIA is often criticized as not being representative of the economy given that it comprises of stocks of only 30, large, mature, and less volatile companies. It has been observed that the DJIA...
-
At the beginning of its fiscal year, Lakeside Inc. leased office space to LTT Corporation under a seven-year operating lease agreement. The contract calls for quarterly rent payments of $25,000 each....
-
Suppose that in addition to investing in one more stock you can invest in T-bills as well. Would you change your answers to Problems 17 and 18 if the T-bill rate is 8%? The correlation coefficients...
-
Is the coupon rate of the bond in Problem 16 more or less than 9%?
-
Do the data suggest a two-factor economy?
-
Show that if two given networks are described by \(Y_{i}=\sum_{j=1}^{M} T_{i j} X_{j}\) and \(Y_{i}^{\prime}=\) \(\sum_{j=1}^{M} T_{i j}^{\prime} X_{j}^{\prime}\), then these networks are...
-
Some FIR filters present a rational transfer function: (a) Show that the transfer function \[H(z)=\frac{\left(r^{-1} z ight)^{-(M+1)}-1}{r e^{\mathrm{j} 2 \pi /(M+1)} z^{-1}-1}\] corresponds to an...
-
Plot the pole-zero constellation as well as the magnitude response of the transfer function of Exercise 4.20 for \(M=6,7,8\) and comment on the results. Exercise 4.20 Some FIR filters present a...
Study smarter with the SolutionInn App