-2
#This is the code I have so far
    

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Write a program that repeatedly asks the user to enter a number, stopping only when they enter 0. Then calculate and display the average of the numbers that were entered. # For this program we will use a while-True-else loop

i = input('Type in any number: __')
total = 0           # accumulator
num = 0             # this will serve as the total number of entries

# We also have to store the quantity of numbers the user has entered
# Maybe include a for loop to count how many times the program was run.
while i != 0:
    print(i)
    total = int(total) + int(i)
    answer = input('Would you like to add a number? ')   # After updating the total, 
                                                         # ask user if they would like to add number.

    if answer == 'y':
        #PROBLEM AREA. Tried continue but only breaks the loop  
    else:
        print('i=0, program is over!')
        print('The total amount: ', total) 
        
    num = num + 1
else:
    print('Program does not run with 0 values')          
    print(total, number, float(total/num))      

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  • Does this answer your question? [Python: How to keep repeating a program until a specific input is obtained?](https://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained) – Gino Mempin Mar 05 '21 at 00:17

1 Answers1

0
nums = []

while True:
    try:
        num = int(input("Input an integer: "))
    except ValueError:
        num = int(input("Input an integer: "))
    
    nums.append(num)
    if num==0:
        break

avg = sum(nums)/(len(nums)-1)
Pooya Kamranjam
  • 355
  • 3
  • 9
  • Yeah, it does thanks! So ValueError just means any integer value except from zero? Will this also work for floats? Also is there a possible way to do it with recurring if loops? Like I was trying to do? – DanLiti Usman Mar 05 '21 at 10:51
  • @DanLitiUsman It checks if the user inputs integer, not string or other data types. You can use `float(input("Input an integer: "))` to cast the input to float. You should increment `i` in each iteration if you want to do it your way `i+=1` – Pooya Kamranjam Mar 06 '21 at 14:48