Question

Problem 1 Write a Python program to do the following: (A) Ask the user to enter...

Problem 1

Write a Python program to do the following:

(A) Ask the user to enter as many integers from 1 to 10 as he/she wants. Store the integers entered by the user in a list. Every time after the user has entered an integer, use a yes/no type question to ask whether he/she wants to enter another one.

(B) Display the list.

(C) Calculate and display the average of the integers in the list.

(D) If the average is higher than 7, subtract 1 from every number in the list. Display the modified list.

The following is an example:

Enter an integer from 1 to 10: 5

Enter another integer? [y/n] y

Enter an integer from 1 to 10: 8

Enter another integer? [y/n] y

Enter an integer from 1 to 10: 9

Enter another integer? [y/n] y

Enter an integer from 1 to 10: 7

Enter another integer? [y/n] y

Enter an integer from 1 to 10: 8

Enter another integer? [y/n] n

Number list: [5, 8, 9, 7, 8]

Average: 7.4

Modified number list: [4, 7, 8, 6, 7]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

The following is the code to read integers from the user, calculate the average and then display the modified list.

I have added comments in the code for your reference.

Please refer to the attached output and screenshot at the end of this answer.


## File: prog.py
def main():

#List to hold the input integers
input_list = []

#Query A
#Loop until we read all the integer inputs from the user
while True:
value = input("Enter an integer from 1 to 10: ")
input_list.append(int(value))

choice = input("Enter another integer? [y/n]: ")
if choice == 'n':
break
  
#Query B
#Print the contents of the list
print("Number List: " + str(input_list))

#Query C
#Calculate the sum and then the Average
sum = 0
for i in input_list:
sum += i

avg = sum / len(input_list);
print("Average: " + str(avg))

#Query D
#If Avg > 7, subtract 1 from each element in the list and then display
if avg > 7:
for i in range(0, len(input_list)):
input_list[i] = input_list[i] - 1


print("Modified List: " + str(input_list))   

if __name__ == "__main__":
main()
  
  
  
  
##### Output
  

Add a comment
Know the answer?
Add Answer to:
Problem 1 Write a Python program to do the following: (A) Ask the user to enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • the user should be prompted to enter an integer and then your program will produce another...

    the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd. The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text. Enter an integer: 1234 Number is even, taking every other digit...

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • write in python Create a program that will ask the user for a list of integers,...

    write in python Create a program that will ask the user for a list of integers, all on one line separated by a slash. construct a list named "adj_numbers" which contains the users input numbers after subtracting 2 from each number. then print the resulting list. the input statement and print statement are given to you in the starting code. a sample run of the program is as follows: Enter a list of integers, separated by slashes: 7/3/6/8 resulting list:...

  • This is a Java text only program. This program will ask if the user wants to...

    This is a Java text only program. This program will ask if the user wants to create a music playlist. If user says he or she would like to create this playlist, then the program should first ask for a name for the playlist and how many songs will be in the playlist. The user should be informed that playlist should have a minimum of 3 songs and a maximum of 10 songs. Next, the program will begin a loop...

  • 1. Write a program to add positive numbers. Ask the user to enter numbers greater than...

    1. Write a program to add positive numbers. Ask the user to enter numbers greater than zero. If a 0, is entered stop requesting numbers and display the total. If a number is entered that is less than 0, display the message ‘Number must be greater than 0’, then ask for another number to be entered. Repeat until a number greater than 0 or 0 is entered. use math lab so I can copy the answers

  • Create two java programs. The first will ask the user to enter three integers. Once the...

    Create two java programs. The first will ask the user to enter three integers. Once the three integers are entered, ask the user to enter one of three functions. The “average” if they want the average of the three numbers entered, enter “min” if they want the minimum and “max” if they want the maximum. Display the answer. Ask the user if they want to calculate another function for the three numbers. If so, loop and ask what function they...

  • RandomGame.cpp (2pt) Write a program that asks the user to enter his/her name. Then begin a...

    RandomGame.cpp (2pt) Write a program that asks the user to enter his/her name. Then begin a do while loop that asks the user to enter a number between 1 and 10. Have the random number generator produce a number 1 and 10. Display the user’s name and both numbers to the screen. Compare the two numbers and report if the entered number is greater than, less than, or the same as the generated number. Ask the user if he/she’d like...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • Write a complete C++ program to ask the user to enter 4 integers. The program must...

    Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the smallest integer among them and print it on the screen. Typical output screen should be as following: Write a complete C++ program to ask the user to enter 4 integers. The program must use a functionto find the smallest integer among them and print it on the screen. Typical output screen should be as following: Note: the code...

  • Help with this please Write an 8086 pasm program which will: Prompt the user to enter...

    Help with this please Write an 8086 pasm program which will: Prompt the user to enter an base-16 (hexadecimal) number from the keyboard. Store the number in the bx register 2) With an appropriate label, print the number in bx as a 16-bit binary value value value value. is Y or 'y' repeat from step 1). 3) With a second appropriate label, print the number in bx as an 8-digit base-4 4) With a third appropriate label, print the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT