#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    New to python, need some help


    Hello all. I would like to thank you in advance for any help you can provide and to say that im sorry that im new to python but over time im hoping to learn more about it.

    My problem: I am trying to write a program that will figure out the average of a certain amount of numbers a user inputs. so if i tell the program i have 3 numbers, it ask what those 3 numbers are and then figures out the average of them. user can input any amount of numbers it wants to average ( instead of them wanting to do 3, it could be 4, 9 etc etc.)

    I'm stuck on trying to figure what to put down so python knows how many numbers the user has inputted.

    here is what i have for code so far

    Code:
    # Average.py
    # A program to compute the average a number of times a user inputs
    
    
    def main():
        print("This is a program to display the average of numbers inputted by user")
        print()
        n = eval (input("Please enter the amount of numbers you want to average:"))
    main()
    i put the last main() on there so i could test what i have so far so ignore that.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    also. i wrote all that in pycharm 2.7.3 and i have python 3.3.2
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    anybody know?

    ive tried a for loop, while loop and some how my while loop turned in to a infinite loop.

    ive got the program to work without using a loop system but for class, it has to have a loop system.

    so if you run the program, it should look like this

    This is a program to display the average of numbers inputted by user

    Please enter the amount of numbers you want to average: ( lets say 3)

    Please enter a number: 2
    Please enter a number: 3
    Please enter a number: 4

    your average for the numbers is: 8
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Keeping in mind that I have no context for what your class is teaching what this lesson is supposed to impart, or what the entirety of your assignment is, here is random advice to improve your code:

    - First, you have the number of times the user wants to enter code. There are a few ways you can make a loop from this information, but one reasonable way is this:
    Code:
    for x in range(n):
       #do something...
    (normally, using x as a variable name would be a bad thing, but you aren't really going to use it here for anything).

    - next, you're going to need to store the entered numbers in some way that can acommodate an arbitrary number of numbers. I'd suggest looking into the python LIST object.

    - finally, whoever is teaching beginners to use "eval" for anything gets this face from me: . Do what you have to for your classes, but NEVER EVER EVER EVER EVER blindly eval() user input in real code. Don't even do it in "just for fun" code. Pretend eval() doesn't exist, because inevitably you'll run into a situation where you think eval() is the solution and it almost certainly will not be.

    If you want to turn user input (which will be a string) into integers, floats, Decimals, etc. then just cast them like so:
    Code:
    int("5432")
    float("42341.3413")

    Hope this steers you in the right direction.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Thank you for your reply, i had just got done getting it figured out. so after 40 hours, i got it figured out lol...

    after much playing around, i noticed that i was really close to the right answer for the program, i was just missing the sum = 0.0.. can somebody explain what this function actually does?

    here is what i got for the program to work
    Code:
    def main():
        print()   # i use prints to separate the lines of text for easier reading.
        print("This is a program designed to figure out what the average "
              "is of the numbers you provide.")
        print()
        g = eval(input("how many numbers do you have to enter?"))
        sum = 0.0
        for i in range(g):
            print()
            f = eval (input("Please enter your number:"))
            sum = sum + f
        print ("The average of the numbers you entered is" , sum /g)
        print("Thank you for using this program")
    main()
    Now this leads me to your question on the eval... I am learning from the python programming: an introduction to computer science ( second edition). by John Zelle.. and its what it tells us to do. so if it is bad, i apologize cause i don't know any better and go off what the book teaches me.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by yobud
    after much playing around, i noticed that i was really close to the right answer for the program, i was just missing the sum = 0.0.. can somebody explain what this function actually does?
    It sets the value of sum to zero. It also effectively declares sum so that you can use it in your loop.

    Now this leads me to your question on the eval... I am learning from the python programming: an introduction to computer science ( second edition). by John Zelle.. and its what it tells us to do. so if it is bad, i apologize cause i don't know any better and go off what the book teaches me.
    As I said, my ire is reserved for the person who thinks it's good to teach newbies eval(), not for you who is just reading and learning. Perhaps within the context of his book Mr. Zelle felt eval() was the most expedient way to teach the concept, but in practical programming eval() is best left alone.

IMN logo majestic logo threadwatch logo seochat tools logo