Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For loops: Code tracing For each of the following questions, predict the code output, and write the answer into the worksheet (and if you

imageimageimage

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.

Step by Step Solution

3.58 Rating (148 Votes )

There are 3 Steps involved in it

Step: 1

1 Given the code for counter in range2 printcounter the output will be 0 1 Explanation The range2 ge... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Investments

Authors: Zvi Bodie, Alex Kane, Alan J. Marcus

9th Edition

73530700, 978-0073530703

More Books

Students also viewed these Programming questions

Question

Is the coupon rate of the bond in Problem 16 more or less than 9%?

Answered: 1 week ago

Question

Do the data suggest a two-factor economy?

Answered: 1 week ago