Mean calculator

This is the place for queries that don't fit in any of the other categories.

Mean calculator

Postby benwhelan94 » Wed Apr 23, 2014 1:36 pm

I'm trying to programme simple mean calculator that creates a programme that looks like this to user ( it cannot use a list or such and has to be like the input below)

Code: Select all
Enter values below (or just press Enter to quit):
 ? 1.5
 ? 2
 ? 4.7
 ? 3.3
 ? 0.8
 ?
 5 values entered
 Mean = 2.46



where if the user doesn't input a number and just presses enter it terminates the loop and prints the mean.
I have two problems;

1) When converting input to float, at the end of loop when user presses enter to end loop, it can't float this 'enter' input. How would i get around this? ( maybe convert 'enter' input to = 0 when adding to total? )
2) How would i count the amount of inputs as to divide the total by this number to get the mean?




My code so far is as below;



Code: Select all
total= 0
input_count=  # don't know what code to use to count the amount of inputs?


while True:
    i=float(input("Enter next number (or just enter to quit): "))
    total += i
   
    print("Your input:", i)
    if not i:
        break
       
print("While loop has exited")
mean = total/input_count



print( str(mean))





Thank for any help, ben
Last edited by stranac on Wed Apr 23, 2014 1:43 pm, edited 1 time in total.
Reason: Added code tags. Locked.
benwhelan94
 
Posts: 19
Joined: Wed Apr 23, 2014 1:25 pm

Re: Mean calculator

Postby stranac » Wed Apr 23, 2014 1:56 pm

benwhelan94 wrote:1) When converting input to float, at the end of loop when user presses enter to end loop, it can't float this 'enter' input. How would i get around this? ( maybe convert 'enter' input to = 0 when adding to total? )

If the user pressed enter(the input == ''), break. Convert to float later.
benwhelan94 wrote:2) How would i count the amount of inputs as to divide the total by this number to get the mean?

Start at 0 and increase by 1 every loop.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Mean calculator

Postby benwhelan94 » Wed Apr 23, 2014 2:05 pm

I get the second bit but i don't know what you mean with the first bit, are you saying i change the condition that when the input == , then it breaks?
benwhelan94
 
Posts: 19
Joined: Wed Apr 23, 2014 1:25 pm

Re: Mean calculator

Postby benwhelan94 » Wed Apr 23, 2014 2:14 pm

Apologies if you started to reply, i have just realised what you meant! It's working now, thanks a bunch!! x
benwhelan94
 
Posts: 19
Joined: Wed Apr 23, 2014 1:25 pm


Return to General Coding Help

Who is online

Users browsing this forum: metulburr, snippsat and 4 guests