HW_W10 (1)

docx

School

San Francisco State University *

*We aren’t endorsed by this school

Course

3000

Subject

Computer Science

Date

Nov 24, 2024

Type

docx

Pages

6

Uploaded by sparrow01

Report
COMP101: Page 1 of 6 Tolulope Adegunju Homework - WEEK 10 [45 points] This is a review of the material from the textbook (Chapter 8 [8.3-8.6]) and lectures from class. You will not be given credit for answers that are copies or near verbatim transcripts – please use your own words and document sources where appropriate using APA guidelines. Chapter 8 (More About Iteration [8.3-8.6]) Week 10 Learning Outcomes Describe the components of a while loop. Use a while loop to solve a problem. Differentiate between definite iteration in a for loop and indefinite iteration in a while loop. Differentiate between when to use a for loop and a while loop in programming. Short Answer Questions (10 points) . Complete the following questions in one or more paragraphs using APA format as required: 1. What is a counter controlled loop? You will need to do some research to determine what a counter controlled loop means in Python. Which kinds of Python loop structures are counter controlled? Provide an example. (2 points) A counter controlled loop is one that repeats itself in a given number of times. The for loop is used in this loop structure. 2. What is a condition controlled loop? You will need to do some research to determine what a condition controlled loop means in Python. Which kinds of Python loop structures are condition controlled? Provide an example. (2 points) with conditioned-controlled loop, it is used when the loop has to be repeated till the condition changes, this loop will repeat a given statement, or a set of statements, if the condition to be met is true. The statement used here is the while statement. To illustrate this, a while statement can be used to calculate commission earned by an employee, the same code will be used to calculate commission for all employees in the organization. 3. What is a loop iteration? Show one using a specific example. (2 points) With loop iteration, this is when the body of a given loop is executed. 4. Does the while loop test its condition before or after it performs an iteration? Explain using a specific example. (2 points)
COMP101: Page 2 of 6 A while loop will test its condition before performing an iteration. If the loop determines that the condition is false, then, the while loop will not be executed. 5. What is an infinite loop? Explain using a specific example. (2 points) An infinite loop can be defined as a loop that does not require a condition to become false for it to terminate. Therefore, an infinite loop will repeat itself continually and it will not terminate since the condition will be true always. Programming Problems (30 points) . All Python programming problems/code snippets for this class are to be written/tested using one of two possible repl.it 1 Python 3 websites. You will work on your code in one of these websites and then copy your final code from the repl.it application and paste it into this Word document 2 for final grading. There are two possible links to be used: one link is for the Python 3 programming problems/code snippets that do not require the Turtle drawing module, and one is for the Python 3 programming problems/code snippets that do require the Python 3 Turtle module. The link you will use for each problem is shown below 3 in square brackets right after the points. Do NOT share or save your code on this website. Also, no screenshots of code will be accepted; you must copy and paste your code into this Word document before submitting. 1. (6 points) [ https://repl.it/languages/python3 ] Write a Python snippet that asks the user over and over again to "Enter a sentence:" (exactly as shown) and accepts a sentence as input. If the sentence is not empty, print the sentence after making all characters in it capitalized; otherwise, print "End" and quit. You will use a while loop here and accept sentences until the user enters an empty string as the sentence (hit Enter after prompt). Hint: you will want to use the upper() function to convert the sentence to all upper case. Program over = input("Enter a sentence:") while len(over) > 0: print (over[0:].upper()) over = input("Enter a sentence:") print ("End") Output 1 The repl.it website is an online tool for running code online in a number of programming languages, including Pyt hon 3. 2 Please make sure your code is in the proper format with proper indenting, etc. after copying it in this document for grading. 3 If a programming problem below requires the Turtle module, then you will need to open the repl.it website that su pports it. Otherwise, you will need to open the repl.it website that does not require this module.
COMP101: Page 3 of 6 2. (6 points) [ https://repl.it/languages/python3 ] Write a Python snippet to ask for an integer n, and print the Fibonacci series between 0 to n. Note : The Fibonacci Sequence is the series of numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, .... Every next number is found by adding together the two numbers before it. Here is an example: Enter integer n: 6 0 1 1 2 3 5 Program n = int(input("Enter integer n: ")) a=0 b=1 T=0 while T<=n: print(a) T = a+b b = a a=T Output
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
COMP101: Page 4 of 6 3. (6 points) [ https://repl.it/languages/python3 ] Write a program that asks the user for three pieces of information: a starting balance (start) , a target balance (target) , and an interest rate (rate, entered as 0.05 for 5%, for example). The program then outputs the number of investment periods required for the starting balance to have grown larger than the target balance (start * (1 + rate)). While this can be computed directly mathematically, we want for this exercise to use a while loop to figure out the answer. Program start = float(input("Enter the starting balance:")) target = float(input("Enter the target balance:")) rate = float(input("Enter interest rate:")) balance = start period = 0 while balance <= target: balance = balance * (1 + rate) period += 1 print (period) Output 4. (6 points) [ https://repl.it/languages/python3 ]
COMP101: Page 5 of 6 Write a Python program that asks the user to respond by entering a value. (Please enter:). The function keeps on asking until the user enters 'Y', 'y', 'yes', 'YES', 'N', 'n', 'no', or 'NO'. Program end = True while end: s = input("Please enter:") if s in ['Y', 'y', 'yes', 'YES', 'N', 'n', 'no', 'NO']: end = False Output 5. (6 points) [ https://repl.it/languages/python3 ] Write a Python program that asks the user to "Enter a number:" [prompt must be this exactly]. The function will keep on asking the user for the number until the sum of the numbers entered is more than 100. Program Sum = 0; while Sum <= 100: num = int(input("Enter a number: ")) Sum = Sum + num; Output Class/Meet Session Reflection (5 points)
COMP101: Page 6 of 6 In two to three paragraphs of prose (i.e. sentences, not bullet lists) using APA style citations as needed, summarize and interact with the content that was covered for this week using the learning outcomes listed above for reference. In your summary, you should highlight the major topics, theories, practices, and knowledge that were covered. Your summary should also interact with the material through personal observations, reflections, and applications to the field of study. In particular, highlight what surprised, confused, enlightened, or otherwise engaged you. In other words, you should think and write critically not just about what was presented but also what you have learned through the session. Feel free to ask questions in this as well since it will be returned to you with answers. (5 points) I learnt when and how to use the for loop and the while loop. I also found helpful resources in the internet, especially from this website: https://python- textbok.readthedocs.io/en/1.0/Loop_Control_Statements.html . after going through this website, I found it a bit easier and I also understood various topics in Python. The internet has been helpful to me since I’ve discovered various sites that explain Python in details. I will like our teacher to be providing these online links to students at the beginning of the course. Additionally, researching online is much and fun as compared to reading textbooks. Also, these online sources areas where students can practice and guide students if they make mistakes. Right now am nervous since we are about to do our final exams and I’ve not yet mastered all the functions, statements among others. Most of the time, I refer to my books, notes, slides among others when am working on my assignments. However, I thank our teacher since his questions help me think and remember most of the things I’ve learnt.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help