CSIS2101_assgn4

docx

School

Nova Southeastern University *

*We aren’t endorsed by this school

Course

2101

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

2

Uploaded by the831unit

Report
Assignment 4: Basic Loops The assignment is another exercise in using a particular language feature, loops in Python. The intention is that you practice writing code with the small tasks. If you copy from other people you are only depriving yourself of an education. To learn, you have to think it through, yourself. Finding a solution online and copying it is easy, but leaves you facing the problem of learning what you were supposed to learn, in the context of a much harder problem the next time. To practice writing loops, create a method called for_while_loop_firstname instead of main. Write The following loops within the for_while_loop_firstname of the same .py program and not as functions. All the loops have to be written using a for loop or a while loop. Turn in this assignment with comments and the header as a zip (or rar) folder as usual that contains only the source code. Write main program called for_while_loop_firstname instead of main which should include the following four loops. 1. Write a loop that counts from -99 down to -113 , printing the count value each time in the loop. The output should be on one line separated by & and look like (notice the spacing then an ampersand(&) between the numbers and an & at the end also): -99 &-100 &-101 &-102 &-103 &-104 &-105 &-106 &-107 &-108 &-109 &-110 &-111 &- 112 &-113& Do this loop using a while loop using a counter_firstname as the counter Call this loop within for_while_loop_firstname(). 2. Write a loop that counts from 0 to 169 by thirteens, printing the count in each iteration but omitting numbers that are divisible by both 13 and 3. The output should look like 0, 13, 26, 52, 65, 91, 104, 130, 143, 169 Note that 39, 78 117 and 156 are omitted as they are divisible by 3 also Notice the commas in between. Notice that there is no comma at the beginning or end. That means that you must do something special either at the beginning or at the end. This should be done using a for loop with your firstname as the target variable for the for-loop. 3. Ask the user to input an integer parameter n which is number of questions on a assignment. The loop should use a for loop with range(n) ( range used with just one parameter) to print out n times “I answered question number k.” where k increases from 1 to n-1. And ending with I am answering question number n.
So For example if input is 5 output should be as shown below: I answered question number 1. I answered question number 2. I answered question number 3. I answered question number 4. I am answering question number 5. 4. Ask the user to input two integer parameters as num1_firstname and num2_firstname The loop should count only the even numbers, between the two given numbers. If input is odd start with the next even number 1 greater than the first input( num1_firstname) and count all the even numbers less than the second input( num2_firstname). If the second numbe(num2_firstname) is less than the first number(num1_firstname) again count down all the even numbers greater than the second number. For this you should use a while loop. The numbers should all appear on the same line. So for example if the first input number passed is 10 (an even number) and the second input number passed is 20, the output would be: 10 12 14 16 18 So for example if the first input number passed is 11 (an odd number) and the second input number passed is 20, the output would be: 12 14 16 18 So for example if the first input number passed is 10 (an even number) and the second input number passed is 0, the output would be: 10 8 6 4 2 So for example if the first input number passed is 11 (an odd number) and the second input number passed is 1, the output would be: 12 10 8 6 4 2 Name the file also as for_while_loop_firstname.py with the loops function, do all these loops within the for_while_loop_firstname function . Do not submit 4 different programs. Use “end = “ to print out different variable counts in a single line inside a loop if they need to be printed on a single line:
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