要从键盘读取十个数字。在此之后写下这些数字中有多少是正数,这些正数的总和,以及正数的平均值(平均值)。
这是我到目前为止所拥有的
print ("Input 10 numbers, separated by commas")
k=[x for x in input("Enter number:").split(',')]
for l in k:
print (l)
if l > 0:
print("positive")
else:
print("negative")
但是我不知道去哪里,我们非常感谢任何帮助!
答案 0 :(得分:0)
您可以定义两个变量positive_total
和positive_count
来跟踪,同时浏览列表
print ("Input 10 numbers, separated by commas")
k=[x for x in input("Enter number:").split(',')]
positive_total = 0 #total of all positive integers
positive_count = 0 #keeps count of number of positive integers
for l in k:
print (l)
if l > 0:
positive_total += l
positive_count += 1
print("positive")
else:
print("negative")
print('Count of positive integers = {}'.format(positive_count))
print('Total of positive integers = {}'.format(positive_total))
print('Average of positive integers = {}'.format(positive_total/positive_count))
答案 1 :(得分:0)
这应该是一个合适的解决方案:
num_list = []
for i in range(10):
num = input("Enter number:")
num_list.append(int(num))
positives = 0
pos_nums = []
for num in num_list:
if num > 0:
positives += 1
pos_nums.append(num)
pos_sum = sum(pos_nums)
print(positives)
print(pos_sum)
print(pos_sum/positives)