September 4th, 2013, 11:37 PM
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
i put the last main() on there so i could test what i have so far so ignore that.
# A program to compute the average a number of times a user inputs
print("This is a program to display the average of numbers inputted by user")
n = eval (input("Please enter the amount of numbers you want to average:"))
September 4th, 2013, 11:38 PM
also. i wrote all that in pycharm 2.7.3 and i have python 3.3.2
September 5th, 2013, 10:01 PM
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
September 6th, 2013, 01:29 AM
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:
(normally, using x as a variable name would be a bad thing, but you aren't really going to use it here for anything).
for x in range(n):
- 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:
Hope this steers you in the right direction.
September 6th, 2013, 01:41 AM
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
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.
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.")
g = eval(input("how many numbers do you have to enter?"))
sum = 0.0
for i in range(g):
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")
September 7th, 2013, 12:47 AM
It sets the value of sum to zero. It also effectively declares sum so that you can use it in your loop.
Originally Posted by yobud
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.