-4

I have an assignment as follows

Write a program that repeatedly asks the user to enter a number, either float or integer until a value -88 is entered. The program should then output the average of the numbers entered with two decimal places. Please note that -88 should not be counted as it is the value entered to terminate the loop

I have gotten the program to ask a number repeatedly and terminate the loop with -99 but I'm struggling to get it to accept integer numbers (1.1 etc) and calculate the average of the numbers entered.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Dio
  • 1
  • 3

3 Answers3

0

the question is actually quite straightforward, i'm posting my solution. However, please show us your work as well so that we could help you better. Generally, fro beginners, you could use the Python built-in data types and functions to perform the task. And you should probably google more about list in python.

def ave_all_num():
    conti = True
    total = []
    while conti:
        n = input('Input value\n')
        try:
            n = float(n)
        except:
            raise ValueError('Enter values {} is not integer or float'.format(n))
        if n == -88:
            break
        total.append(n)
    return round(sum(total)/len(total),2)

rslt = ave_all_num()
MaThMaX
  • 1,995
  • 1
  • 12
  • 23
  • please do not enable improper questions by answering them – Mad Physicist Feb 21 '18 at 05:59
  • Hi MaThMax, Thank you for your input. My code is below: ------------------------------------------------------------------------ #Assignment2, Question 3 numbers=[] while True: num=int(input("Enter any number:")) if num==float: continue if num==-88: break return print(" the average of the numbers entered are:",sum(numbers)/len(numbers) – Dio Feb 21 '18 at 06:29
0

enter code hereThank you for the prompt replies. Apologies. This is the code i was working on:

`#Assignment2, Question 3

numbers=[]

while True:
    num=int(input("Enter any number:"))
    if num==float:
        continue
    if num==-88:
        break
return print(" the average of the numbers entered are:",sum(numbers)/len(numbers)`
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52
Dio
  • 1
  • 3
0

Try the following python code. =)

flag = True

lst=[]
while(flag):
    num = float(raw_input("Enter a number. "))
    lst+=[num]
    if(num==-88.0): flag = False     

print "Average of numbers: ", round( (sum(lst[:-1])/len(lst[:-1])) , 2)
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52