+1 (315) 557-6473 

Python Program to Create 3-Programs-With-Loops Assignment Solution.


Instructions

Objective
Write a program to create 3 programs with loops in python.

Requirements and Specifications

Assignment:
  •  For this homework, you will write 3 programs. Each should be a separate program, and each should run separately.
Program 1
Write a python assignment that asks the user to input two integer values between 0 and 100. Using a WHILE loop, the program should then print the all the numbers between the numbers the user input (specifically- NOT including the beginning and end numbers) AND the sum of all of those numbers. For example, if the first number the user inputs is 4, and the second number the user inputs is 23, the program should print:
 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
243
Program 2
Your task for this program is the same as Program 1: Write a program that asks the user to input two integer values between 0 and 100, and print the sum of all the numbers between the numbers the user input. For this program, however, use a FOR loop instead of a WHILE loop.
For loops are covered in section 4.6 of “Python for Everyone”.
In addition to the book, I suggest watching BOTH of the following videos:
https://youtu.be/94UHCEmprCY                              (especially the first 5 minutes)
And websites:
https://wiki.python.org/moin/ForLoop                    (I suggest running the code they show you in the first examples!!!)
Program 3
Write a program that asks the user for any number of floating point numbers. When the user is done entering numbers, they should enter “done”. The program should then print to the screen the count of the numbers entered, the sum of the numbers entered, and the average of the numbers entered.
Guidelines:
  •  For each program, create a header for the user interactions within your file. The header should state your name, the assignment name/number, and the date.
  •  ALL of your code must always be within this file- you will lose points if you attempt to import libraries or code that is external to the file (I’m happy if you can do this, but for now let’s keep it simple, k?)
  •  Give priority to writing CODE THAT WORKS. If you can’t get part of the code to run, comment out that part and tell me (in the comment) what you were trying to do so I can give you partial credit. Try hard to avoid giving me code that doesn’t work- I shouldn’t have to debug your code just to get it to run.
  •  I suggest you use comments liberally
Submission Instructions:
  • Submit your code as NOTEPAD (or other text) files. NOT WORD.
  • Your file should include:
  • your code (at the top of the file) and
  • your output (just do a capture of the output. Please come up with your own test data. )
TIPS
Do NOT just start trying to hack Python code. I suggest you start with a human level walk-through, then a program-level walk-through, then a flow chart, and then pseudocode.
Once you have the flow of the program nailed down, THEN convert it to Python code.
Start early- you don’t want to wait until the night before the assignment is due to get started on this homework.
Keep it simple.
Source Code
PROGRAM 1
# Print instructions
print("Welcome! In this program, you must enter two numbers n1 and n2 such that n2 > n1.")
print("Then, the program will print the total numbers in the range (n1, n2) and the sum of all numbers.")
print("Note that, the range to be printed is (n1, n2) and not [n1, n2], so the numbers n1 and n2 are not inclusive.")
print()
n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))
if n2 <= n1:
    print("Sorry, the second number must be higher than the first number.")
else:
    #Now print numbers starting at n1+1
    current_number = n1+1
    total_sum = 0 # store the sum of the numbers here
    while current_number < n2:
        total_sum += current_number
        print(current_number, end="")
        # If the current number is less than n2, print a comma
        if current_number < n2-1:
            print(", ", end="")
        current_number += 1
    # Now print sum
    print("")
    print(total_sum)
PROGRAM 2
# Print instructions
print("Welcome! In this program, you must enter two numbers n1 and n2 such that n2 > n1.")
print("Then, the program will print the total numbers in the range (n1, n2) and the sum of all numbers.")
print("Note that, the range to be printed is (n1, n2) and not [n1, n2], so the numbers n1 and n2 are not inclusive.")
print()
n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))
if n2 <= n1:
    print("Sorry, the second number must be higher than the first number.")
else:
    #Now print numbers starting at n1+1
    current_number = n1+1
    total_sum = 0 # store the sum of the numbers here
    # Create a for loop that starts at n1 and ends at n2-1
    for n in range(n1+1, n2):
        total_sum += n
        print(n, end="")
        # If the current number is less than n2, print a comma
        if n < n2-1:
            print(", ", end="")
        n += 1
    # Now print sum
    print("")
    print(total_sum)
PROGRAM 3
# Print instructions
print("Welcome! In this program, you must enter floating-point numbers, or the word \"done\" if you want to exit.")
print("The program will then calculate the total sum, average and count of the numbers entered.")
print()
# To keep user asking for a number, I will use a while-loop
total_sum = 0 # store total sum of the numbers here
count = 0 # store the count of numbers here
while True:
    # Ask user for number
    user_input = input("Enter a floating point number or \"done\" to exit: ")
    if user_input == "done": # user entered "done"
        break
    else: # if not, then the user entered a floating-point number
        number = float(user_input)
        total_sum += number
        count += 1
# If we reach this, it is because the user entered "done"
if count > 0:
    # calculate average
    avg = total_sum / count
    print("Total sum: ", total_sum)
    print("Average: ", avg)
    print("Total numbers: ", count)
else:
    print("You did not enter any number.")