Question

In: Computer Science

"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...

"PYTHON"

Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out:

a) the sum of all the numbers

b) the average of all the numbers

c) the max of the numbers

d) the min of the numbers

Note we did not cover lists (array) so you will not use them in this problem.

Finally, ask the user for their name, then print their name as many times as the max value (determined earlier) . Note, I want you to use a for loop for this part.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

EDIT: updated the code to include counters for printing name

#code

def main():
    #initializing sum, count, average to 0
   
sum=0
    count=0
    avg=0
    #initializing two variables to store max and min values to None
   
maxVal=None
   
minVal=None

   
#looping indefinitely
   
while True:
        #reading a number
       
num=int(input("Enter a number (negative to end): "))
        #exiting the loop if the number is negative
       
if num<0:
            break
       
#adding num to sum
       
sum+=num
        #updating count
       
count+=1
        #if maxVal is not initialized or if it is greater than current maxVal, updating
        #maxVal to current value
       
if maxVal is None or num>maxVal:
            maxVal=num
        #doing the same for minVal, checking if num is less than minVal
       
if minVal is None or num<minVal:
            minVal=num
    #after exiting loop, if count is above 0, finding average
   
if count>0:
        avg=sum/count
    #printing stats
   
print("Sum of numbers:",sum)
    print("Average of numbers:",avg)
    print("Maximum of numbers:", maxVal)
    print("Minimum of numbers:", minVal)
    #reading name
   
name=input("Enter your name: ")
    #using a for loop, printing name maxVal times
   
for i in range(maxVal):
        print(str(i+1)+". "+name)

#invoking main()
main()


Related Solutions

Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. Example: Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT