# Python while loop | Computer Science homework help

# Name: # # While Loop Exercise # # Overview: # * This assignment contains "questions" each comprised of # a print statement that specifies what the output for # that "question" should look like. # * Write a single while loop for each "question". # # Instructions: # * Copy this file and add while statements to get the # specified behavior. # * Do not delete anything from this file. # * Fill in your name at the top of the file. # * The first exercise is implemented as an example. # * Implement the while loops one at a time in the # order they occur in the file. # * Test your program with various inputs and make sure # each loop is correct before going on to the next. # * If the end value is not in the sequence then the last # number output should be the last number in the sequence # less than the end value. # # Requirements: # * See the requiremets on the assignment page. # # Points will be deducted for requirements not fulfilled. # # Run notes: # * The input will cause the prompt message to be output # and will wait for the user to type a number. # * After the input statement has executed the variable limit # will have for its value the value the user typed. # * If, for example, limit has the value 10 the the expression # (limit - 1) is 9, thus 1 2 3 ... (limit - 1) means # 1 2 3 4 5 6 7 8 9 # # Print command notes: # * Use a print similar to the one given in the first loop # to cause a space only (on newline) between each output # # Implementation notes: # * DO NOT CHANGE THE VALUE OF THE VARIABLE limit. # * If the sequence is not clear plug a specific value in # for limit (8 or 10 for example) and see what it looks like. # * If the last value specified for output not in the # sequence then output then output the value that # occurs in the sequence immediately before it # as the last value output. # # Additional notes for specific loops: # 9. What happens if limit is odd? (Just observe what happens.) # 12. Example output with limit of 10: 10 7 4 1 # 13. Example output with limit of 6: -6 -5 -4 -3 -2 -1 0 # 15. Example output with limit of 6: -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 # 16. The output will be floats due to the necessary division. # Example output with limit of 10: -5 -4 -3 -2 -1 0 # What happens if limit is odd? (Just observe what happens.) # 17. The output will be floats due to the necessary division. # Define and use a variable named half_limit # What happens if limit is odd? (Just observe what happens.) # 18. Do not use the exponentiation operator **. # Use the counter variable to compute each square. # 19. Output limit number of factorials. # ! denotes factorial, example: # 5! = 1 * 2 * 3 * 4 * 5 # Use a variable named factorial to accumulate the factorial in. # Each iteration use the accumulated value and the loop # counter to compute the next factorial value. # 20. Output powers of 2. # There should be limit number of powers output. # Do not use the exponentiation operator **. # Use a variable named powers to accumulate the powers in. # (Each iteration use the accumulated value and # compute the next power of 2 value.) # 21. Output a single value only, the sum. # If the value in limit is 4 the output value should be 10 (1 + 2 + 3 + 4). # Use a variable named sum to accumulate the sum in. # (Each iteration use the accumulated value and the loop # counter to compute the next sum value.) # #========================================================== # Prompt, input a value limit = input('Enter a value for limit: ') limit = int(limit) # Output the value of limit print("limit =", limit ) #========================================================== print('1.) Output: 1 2 3 4 ... (limit - 1)') ctr = 1 while ctr <= (limit - 1): print(ctr, end=' ') ctr = ctr + 1 print('\n\n2.) Output: 1 2 3 4 ... limit') print('\n\n3.) Output: 0 1 2 3 ... limit') print('\n\n4.) Output: 0 1 2 3 ... (limit - 1)') print('\n\n5.) Output: 0 2 4 6 ... limit') print('\n\n6.) Output: 1 3 5 7 ... (limit - 1)') print('\n\n7.) Output: limit ... 3 2 1 0') print('\n\n8.) Output: (limit - 1) ... 4 3 2 1') print('\n\n9.) Output: limit ... 6 4 2 0') print('\n\n10.) Output: 0 3 6 9 ... limit') print('\n\n11.) Output: 1 4 7 10 ... limit') print('\n\n12.) Output: limit (limit - 3) (limit - 6) ... 0') print('\n\n13.) Output: -limit, -limit + 1, -limit + 2 ... 0') print('\n\n14.) Output: -limit, -limit + 1, -limit + 2 ... (limit - 1) limit') print('\n\n15.) Output: -limit, -limit + 2, -limit + 4 ... (limit - 2) limit') print('\n\n16.) Output: -(limit / 2) ... -2 -1 0') print('\n\n17.) Output: -(limit / 2) ... -2 -1 0 1 2 ... (limit / 2)') print('\n\n18.) Output: 1 4 9 16 25 ... limit_squared') print('\n\n19.) Output: 1 2 6 24 120 ... limit!') print('\n\n20.) Output: 1 2 4 8 16 32 64 128 256 ...') print('\n\n21.) Output the sum: 1 + 2 + 3 + 4 + ... + limit')