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.
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 (other than the sentinel). Do the following: 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers You must use alternation ('if' statements), loops and simple calculations to do...
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)
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
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.
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.
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.
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT