On Fri, 5 Mar 2004, Rodolfo Rosario wrote:
i`m a TOTAL newbie in programming in general and in python...i`m learning
python because it`s the easier programming language around(at least they
told me) and that it`s easy to enter the world of programming with it...i am
reading "INSTANT HACKING" and i`m stuck with one of the exercises, i don`t
know how to do it, please help me with it...
Write a program that continually reads in numbers from the user and adds
them together until the sum reaches 100.
I really don`t know how to sum what the user types...:(
given by the Instant Hacking website you know already how to get input
from the user.
number = input("Type in a number: ")
BTW: it's a pity that the author of this site chooses to use input
instead of raw_input. The function input takes the user input *and evals
it immidiatly" - that means: use it as a command. That's not good in
respect of security and not good in respect of usability. input is more
a function for "writing a quick script for expert use" to state from the
Library Reference Docs.
It's better to use raw_input and get strings from the user back:
number_as_string = raw_input("Type in a number: ")
number = int(number_as_string) # turn string into integer
From here on, all you have to do is to ask the user for that many
numbers, until their sum reaches 100. You will have to use some kind of
a loop (I leave decission on "for" or "while" to you - that's a main
part of this exercise and I wouldn't help you if I solve it for you :-),
track the current sum in a variable and break the loop when your sum is
100 or above ('above' would be the wrong result but the only way to know
"until 100" is reached. You will need to print out the sum as it was
before growing larger than 100. The easy case is when your user hits
exactly the 100).