Design a program that asks the user to enter a series of 20 numbers. The program
ID: 3589651 • Letter: D
Question
Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in a list and then display the following data:
lowest number in list, highest number in list, total of numbers in list, average of numbers in list.
The following is what I have for the program. I keep getting an error message that "print" is an invalid syntax. If this is corrected, is the rest of the program written correctly?
#main
def main():
#create empty list
twenty_number_list = []
#loop variable
another = 'Y'
#prompt user for numbers and add to list
while another == 'Y':
value = float(input('Enter a number: '))
twenty_number_list.append(value)
#prompt user for another number
print('Do you want to add another number?')
another = input('Y = yes, anything else = no: ')
#initialize variable with first element of array
min_value=twenty_number_list[0]
max_value=twenty_number_list[0]
#set total to zero
total=0
#calculate total using for loop and average
for i in range(20):
total+=int(twenty_number_list[i]
#printing data
print('The lowest number is: '+str(min_value))
print('The highest number is: '+str(max_value))
print('The total value is: '+str(total))
print('The average value is: '+str(average))
#calling main
main()
Explanation / Answer
Here is the fixed code:
#main
def main():
#create empty list
twenty_number_list = [0]*20
#loop variable
#prompt user for numbers and add to list
for i in range(0, 20):
value = float(input('Enter ' + str(i+1) + 'th number: '))
twenty_number_list[i] = value
#initialize variable with first element of array
min_value=min(twenty_number_list)
max_value=max(twenty_number_list)
#set total to zero
total=0
#calculate total using for loop and average
for i in range(20):
total+=twenty_number_list[i]
average = total/20.0
#printing data
print('The lowest number is: '+str(min_value))
print('The highest number is: '+str(max_value))
print('The total value is: '+str(total))
print('The average value is: '+str(average))
#calling main
main()
# code link: https://paste.ee/p/tc7Eb
Basically question do not ask for any number of numbers but for exact 20 numbers
you are not computing, min and max but using only first value
You are not computing average
indentation was completely messed up.
Brackets were not closed properly.
You are unncessarily coverting float to int
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.