36.8k views
3 votes
In python please!! Write the definition of a function named countPos that needs integer values from standard input until there are none left and returns the number that are positive. The function must not use a loop of any kind.

User JanMer
by
6.0k points

1 Answer

6 votes

Answer:

Step-by-step explanation:

The following code is written in Python it doesn't use any loops, instead it uses a recursive function in order to continue asking the user for the inputs and count the number of positive values. If anything other than a number is passed it automatically ends the program.

def countPos(number=input("Enter number: "), counter=0):

try:

number = int(number)

if number > 0:

counter += 1

newNumber = input("Enter number: ")

return countPos(newNumber, counter)

else:

newNumber = input("Enter number: ")

return countPos(newNumber, counter)

except:

print(counter)

print("Program Finished")

countPos()

In python please!! Write the definition of a function named countPos that needs integer-example-1
User Fasermaler
by
5.7k points