averages calculator

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

averages calculator

Postby Hissing Cockroach » Fri Feb 07, 2014 5:58 pm

Hi. I am wanting to make a program that calculates averages. I've got it,
but I'm not sure how to make it stop asking for numbers after you press "e".
whenever I do press e, it just doesn't do anything at all.
I'm thinking it's because you can't include if statements in functions, but I don't want
to type an if statement after every prompt for numbers. I also want to fix that. I know there's
a better way than to just keep typing "inpt = raw_input("insert another number")".
Could anyone help with this? Here is the code I'm working on:

Code: Select all
start = False
ready = False
num = 0
def check():
    if num == str("e"):
        start = True
       
while True:
    inpt = raw_input("insert number")
    num += 1
    check()
    inpt1 = raw_input("insert another number")
    num += 1
    check()
    inpt2 = raw_input("insert another number")
    num += 1
    check()
    inpt3 = raw_input("insert another number")
    num += 1
    check()
    inpt4 = raw_input("insert another number")
    num += 1
    check()
    inpt5 = raw_input("insert another number")
    num += 1
    check()
    inpt6 = raw_input("insert another number")
    num += 1
    check()
    start = True
 
   
    if start:
        add = int(inpt) + int(inpt2) + int(inpt3) + int(inpt4) + int(inpt5) + int(inpt6)
        answer = add / num

        print answer
    raw_input()
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: averages calculator

Postby stranac » Fri Feb 07, 2014 6:27 pm

Code: Select all
    if num == str("e"):

num is a number. It has nothing to do with your inputs.
Hissing Cockroach wrote:I don't want
to type an if statement after every prompt for numbers. I also want to fix that. I know there's
a better way than to just keep typing "inpt = raw_input("insert another number")"

Use a 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: averages calculator

Postby Yoriz » Fri Feb 07, 2014 6:32 pm

Call
Code: Select all
inpt = raw_input(input_srt)
only once and let the while loop take care of additional inputs
Try and add the entered value to the total.
if there is an error find out if it was because "e" was entered, if so break the loop, if not ask for correct input.

Code: Select all
num = 0
total = 0


input_srt = "insert number"

while True:
    inpt = raw_input(input_srt)
    try:
        total += int(inpt)
        num += 1
        input_srt = "insert another number"
    except ValueError:
        if inpt.lower() == "e":
            break
        print "Please enter a int only"


if num > 0:
    print total / num
raw_input('Finished')
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 838
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: averages calculator

Postby Hissing Cockroach » Fri Feb 07, 2014 8:44 pm

Thanks! That really helped!
It also prompted me to learn what the "try", and "except" statements were, as
well as a few other commands.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests