I've been tasked by my programming professor to create a program that calls for a user to enter as many positive integers (including zero) as they care to enter. Anything other than a positive integer stops input and calculates/displays the Sum, Average, and Count of the numbers.
The main issue I'm having has to do with returning multiple positive integers to be passed across the program. I can't seem to find what I need.
inputPostiveInteger()
Pass in nothing
Ask the user for a positive integer
Return the integer is the user inputs one
Return -1 if the user does not input a positive integer
The way he had the program shown to work would be example -
Enter a positive integer, anything else to quit: 1
Enter a positive integer, anything else to quit: 5
Enter a positive integer, anything else to quit: 10
Enter a positive integer, anything else to quit: cat
Sum 16
Average 5.3
Total numbers 3
I've tried multiple ways of try/except and while not clauses to get the input portion correct but can't seem to wrap my brain around this one.
I understand the formulas in how to calculate / display the average, counts, sum - but can't seem to get vetted integer entries down inside of multiple formulas.
Ex. for attemping the inputs
def inputPositiveInteger():
try:
userInt = int(input("Enter a positive integer, anything else to quit: ")) > -1
except ValueError:
return -1
def main():
total = 0
count = 0
posInt = inputPositiveInteger()
while posInt != -1:
total += posInt
count += 1
main()
total = 0
userInt = int(input('Enter a positive integer, anything else to quit: '))
while userNumber > -1:
total += userInt
userInt = int(input('Enter a positive integer, anything else to quit: '))