Question

In: Computer Science

**Python** 4. Running total of product and sum of integers input by a user Write a...

**Python**

4. Running total of product and sum of integers input by a user

Write a function ProdSum that asks a user to input a number from one to 99. If the input number is less than or equal to 25, then calculate the running product of each number inputted by the user that is less that or equal to 25. If the number is greater than 25, then calculate the running sum of each number inputted by the user that is greater that 25. If the user enters zero as input, then print out the total running product calculated and the total running sum calculated and end the function.

Assume the user can input as many numbers as desired until inputting a zero. Make sure you test the number inputted by the user to ensure it is in the range from zero to 99 and inform the user if the number is not in the range and to try again. Also print the appropriate labels for the two calculations in your print statements, as well as the number inputted by the user..

For Example:

Enter a number from 1 to 99 or 0 to Exit: 5

Enter a number from 1 to 99 or 0 to Exit: 83

Enter a number from 1 to 99 or 0 to Exit: 21

Enter a number from 1 to 99 or 0 to Exit: 55

Enter a number from 1 to 99 or 0 to Exit: 13

Enter a number from 1 to 99 or 0 to Exit: 64

Enter a number from 1 to 99 or 0 to Exit: 0

The product of the numbers [5, 21, 14] is 1470

The sum of the numbers [83, 55, 63] is 201

Solutions

Expert Solution

def ProdSum():
    product = 1
    total = 0
    product_list = []
    total_list = []
    while True:
        num = int(input("Enter a number from 1 to 99 or 0 to Exit: "))
        if 0 <= num <= 99:
            if num == 0:
                break
            elif num <= 25:
                product_list.append(num)
                product *= num
            else:
                total_list.append(num)
                total += num
        else:
            print("It's not in the valid range.")
    print("The product of the numbers", product_list, "is", product)
    print("The sum of the numbers", total_list, "is", total)


ProdSum()


Related Solutions

Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write a program that computes the product of two fractions. The user provides 4 input which...
Write a program that computes the product of two fractions. The user provides 4 input which represent the numerator and the denominator of two fractions and prints the result of the operation. The program uses two functions. One called MultiplyNumerator(…) which calculates the product of the numerators of the fractions, and a second one called MultiplyDenom(…) which calculates the product of the denominator of the fractions. Call your program MultiplyFrac.cpp
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
write a program that takes the input value from the user and calculate the sum from...
write a program that takes the input value from the user and calculate the sum from that number to zero in MIPS
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT