Write a program to read integers that do not specify numbers, then count how many positive numbers and negative numbers are in the read integers and calculate the average value of these input values.
Input format:
Enter several integers in a line, separated by commas.
Output format:
Output the number of positive and negative numbers on the first line, separated by commas.
Output their average value on the second line (preserve the two decimal places, such as: 12.30).
Enter sample:
Here is a set of inputs. For example:
12,3,-2,-56,100,62
Output sample:
The corresponding output is given here. For example:
4,2
19.83
Answer:
list_average = input().split(",") i = 0 for x in list_average: list_average[i] = int(x) i += 1 sum = 0 for x in list_average: sum += x average = sum / len(list_average) negative = 0 positive = 0 for x in list_average: if x < 0: negative += 1 elif x > 0: positive += 1 print(f"{positive},{negative}") print(f"{average:.2f}")
Use python: enter 20 integers to count the number of positive, negative and zero
# Enter 20 integers to count the number of positive, negative and zero num = 1 positive = 0 negative = 0 zero = 0 while num <= 20: a = int(input()) num += 1 if a > 0: positive += 1 elif a < 0: negative += 1 else: zero += 1 print(positive) print(negative) print(zero)
Summarize
This is the end of this article about how to use Python to count positive and negative numbers. For more related content on Python to count positive and negative numbers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!