Implement a Python function called countpos that takes a lis

Implement a Python function called count_pos that takes a list of numbers as input (parameter) and returns the number of elements of that list that are positive (> 0). Then, in the main, your program should ask the user to input the list, then it should call count_pos function with that list, and print the result. You may assume that the user will always enter at least two elements, but your function count_pos should work for all lists of numbers including those of length 0 and 1.

Examples of program runs:

Example 1:

Please input a list of numbers separated by commas: 1,2,0,3,0,4

There are 4 positive numbers in your list.

Example 2:

Please input a list of numbers separated by commas: 1,-2,9

There are 2 positive numbers in your list.

Here is a way to ask a user for a list:

s = input(\"Please input a list of numbers separated by commas: \")

l = list(eval(s))

If a user enters at least two numbers separated by commas, variable l will refer to that list.

Solution

s = input(\"Please input a list of numbers separated by commas: \");
l=list(eval(s));
s=s.split(\',\')## split the string with commas
count=0; ## initialising count
s = list(map(int, s))## converting string list to int list
for i in range(len(s)):
if s[i]>0:## comparing list with -ve numbers
count=count+1;## taking -ve numbers count
print(\"There are\",count,\"positive numbers in your list\");

************************ output********************

Please input a list of numbers separated by commas: 1,2,0,3,0,4
There are 4 positive numbers in your list
>>> ================================ RESTART ================================
>>>
Please input a list of numbers separated by commas: 1,-2,9
There are 2 positive numbers in your list
>>> ================================ RESTART ================================
>>>
Please input a list of numbers separated by commas: 1,-2,9,-3,-4,4,4,5
There are 5 positive numbers in your list
>>>

Implement a Python function called count_pos that takes a list of numbers as input (parameter) and returns the number of elements of that list that are positive

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site