Nhu Hung H. answered 05/04/24
All-level Maths, English test prep and Programming Tutor
- Basic answer: You must analyze the steps this program requires. First you must repeatedly ask the user to input each number and store each number securely. Next you must process the numbers, checking each number if they are positive or negative and count them and also sum them up for later step. Finally, you must print the number of positives, negatives, total, and also the mean, which is the sum calculated earlier divided by the number of numbers. Notice that you don't have to do this processing if there is only the number 0 in the list.
# Ask for input
# It's a repetitive task, so you must use either a for or a while loop. Since you cannot anticipate the number of times this will loop, you can only use the while loop.
# As you have to securely store the numbers to process further, you must create a list to store them.
list_of_numbers = list()
while True:
# Don't forget to cast your number to integers, instead of strings
number = int(input('Enter an integer, the input ends if it is 0'))
# Check if you need to store this number to the list, or it's a 0 and you have to stop asking for input.
if number == 0:
list_of_numbers.append(number)
else:
# If user inputs 0, it means he/she has done inputting, and we can start processing. So we will break out of the loop
break
# Next you should start processing
# Or should you? Remember that you need not process if the user only input 0. What is the indicator of this? The list is empty, or in other word, the length is 0.
if len(list_of_numbers) == 0:
print("No numbers are entered except 0")
else:
# Now you may start processing
# You need to count the number of positives and negatives. And since you have to process them further (printing them), you must store them into variables. These variables must be able to perform summation and determine the number of items within them. A list is okay for this purpose
positives = list()
negatives = list()
# Then you will read each number and check if they are positive/negative, then add them to the suitable list.
for number in list_of_numbers:
if number > 0:
positives.append(number)
# This else should mean unless the number is greater than 0, which means it can be 0 or less. However, since we are sure the list does not contain 0, we can safely ignore accidentally processing 0.
else:
negatives.append(number)
# We can now print the results
print('The number of positives is', len(positives))
print('The number of negatives is', len(negatives))
print('The total is', sum(list_of_numbers))
print('The average is', sum(list_of_numbers)/len(list_of_numbers))
- A bit more advanced answer: you can process at the same time you ask for input, but this will make the program more error-prone if you update your code in the future. And as list_of_numbers only has positive and negative numbers, the number of negatives is the difference between the numbers of all numbers and the number of positives.
- An advanced answer: you can use the Python-native function filter to filter out the postives/negatives in the list, without the need for a for loop. Functions such as reduce can also be used.