Beginner Programmer Question | | |
I am doing alot of reading and trying to teach myself how to program.
I can not figure out how to make "Write a program that continually
reads in numbers from the user and adds them together until the sum
reaches 100." this work. If someone could show me the correct code so i
can learn from that it would be much appreciated. Thanks | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> I am doing alot of reading and trying to teach myself how to program.
> I can not figure out how to make "Write a program that continually
> reads in numbers from the user and adds them together until the sum
> reaches 100." this work. If someone could show me the correct code so i
> can learn from that it would be much appreciated. Thanks
>[/color]
Isn't it your homework?
Why can't you figure it out?
What have you tried?
Claudio | | | | re: Beginner Programmer Question
Claudio Grondi wrote:[color=blue]
> kydavis77@gmail.com wrote:[color=green]
> > I am doing alot of reading and trying to teach myself how to program.
> > I can not figure out how to make "Write a program that continually
> > reads in numbers from the user and adds them together until the sum
> > reaches 100." this work. If someone could show me the correct code so i
> > can learn from that it would be much appreciated. Thanks
> >[/color]
> Isn't it your homework?
> Why can't you figure it out?
> What have you tried?
>
> Claudio[/color]
I am doing alot of reading, and the problem didnt come with an answer.
I dont understand how to get it to continually input numbers and add
all those together | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> Claudio Grondi wrote:[color=green]
> > kydavis77@gmail.com wrote:[color=darkred]
> > > I am doing alot of reading and trying to teach myself how to program.
> > > I can not figure out how to make "Write a program that continually
> > > reads in numbers from the user and adds them together until the sum
> > > reaches 100." this work. If someone could show me the correct code so i
> > > can learn from that it would be much appreciated. Thanks
> > >[/color]
> > Isn't it your homework?
> > Why can't you figure it out?
> > What have you tried?
> >
> > Claudio[/color]
>
> I am doing alot of reading, and the problem didnt come with an answer.
> I dont understand how to get it to continually input numbers and add
> all those together[/color]
#Add up to 100 program
#What number are you adding up to?
bigone = 100
number = input("Whats the first number?")
number2 = input ("Whats the second number?")
nu3 = number+number2
while nu3 < bigone:
print ("Not there yet, next number please")
print "Finally there!"
thats what i thought maybe it was...but after the first two numbers it
just continually scrolls on the screen with finally there | | | | re: Beginner Programmer Question
>[color=blue]
> I am doing alot of reading, and the problem didnt come with an answer.
> I dont understand how to get it to continually input numbers and add
> all those together[/color]
Use while, raw_input, sys.argv[1] and int() and break the loop when the
sum is above 100.
;-) | | | | re: Beginner Programmer Question
Rune Strand wrote:[color=blue][color=green]
> >
> > I am doing alot of reading, and the problem didnt come with an answer.
> > I dont understand how to get it to continually input numbers and add
> > all those together[/color]
>
> Use while, raw_input, sys.argv[1] and int() and break the loop when the
> sum is above 100.
>
> ;-)[/color]
thanks for the help..but i am extremley new and what you said makes no
sense to me | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> I am doing alot of reading and trying to teach myself how to program.
> I can not figure out how to make "Write a program that continually
> reads in numbers from the user and adds them together until the sum
> reaches 100." this work. If someone could show me the correct code so i
> can learn from that it would be much appreciated. Thanks
>[/color]
summ = 0
while summ < 100:
usr = input('Enter a number:')
summ += usr #that's same as>>> summ = summ + usr
print summ
print "now continue with whatever you want..." | | | | re: Beginner Programmer Question
Rune Strand wrote:[color=blue][color=green]
>> I am doing alot of reading, and the problem didnt come with an answer.
>> I dont understand how to get it to continually input numbers and add
>> all those together[/color]
>
> Use while, raw_input, sys.argv[1] and int() and break the loop when the
> sum is above 100.
>
> ;-)
>[/color]
I don't think you understood his problem. He is trying to learn how to
*program*, not just learn Python ;) | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> Rune Strand wrote:[color=green][color=darkred]
> > >
> > > I am doing alot of reading, and the problem didnt come with an answer.
> > > I dont understand how to get it to continually input numbers and add
> > > all those together[/color]
> >
> > Use while, raw_input, sys.argv[1] and int() and break the loop when the
> > sum is above 100.
> >
> > ;-)[/color]
>
> thanks for the help..but i am extremley new and what you said makes no
> sense to me[/color]
In the code you posted above here: Move the input into the while loop.
You may prefer raw_input() to input().
I don't want to write the code for you ;-) | | | | re: Beginner Programmer Question
Rune Strand wrote:[color=blue]
> kydavis77@gmail.com wrote:[color=green]
> > Rune Strand wrote:[color=darkred]
> > > >
> > > > I am doing alot of reading, and the problem didnt come with an answer.
> > > > I dont understand how to get it to continually input numbers and add
> > > > all those together
> > >
> > > Use while, raw_input, sys.argv[1] and int() and break the loop when the
> > > sum is above 100.
> > >
> > > ;-)[/color]
> >
> > thanks for the help..but i am extremley new and what you said makes no
> > sense to me[/color]
>
> In the code you posted above here: Move the input into the while loop.
> You may prefer raw_input() to input().
>
> I don't want to write the code for you ;-)[/color]
whats the difference between raw input and input? | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> kydavis77@gmail.com wrote:
>[color=green]
>>Claudio Grondi wrote:
>>[color=darkred]
>>>kydavis77@gmail.com wrote:
>>>
>>>>I am doing alot of reading and trying to teach myself how to program.
>>>>I can not figure out how to make "Write a program that continually
>>>>reads in numbers from the user and adds them together until the sum
>>>>reaches 100." this work. If someone could show me the correct code so i
>>>>can learn from that it would be much appreciated. Thanks
>>>>
>>>
>>>Isn't it your homework?
>>>Why can't you figure it out?
>>>What have you tried?
>>>
>>>Claudio[/color]
>>
>>I am doing alot of reading, and the problem didnt come with an answer.
>>I dont understand how to get it to continually input numbers and add
>>all those together[/color]
>
>
> #Add up to 100 program
>
> #What number are you adding up to?
> bigone = 100
>
> number = input("Whats the first number?")
> number2 = input ("Whats the second number?")
> nu3 = number+number2
> while nu3 < bigone:[/color]
# you are missing the input and building the sum here:
number3 = input ("Whats the next number?")
nu3 = nu3 + number3[color=blue]
> print ("Not there yet, next number please")[/color]
# so nu3 gets never changed and your loop runs forever.[color=blue]
>
>
> print "Finally there!"
>
> thats what i thought maybe it was...but after the first two numbers it
> just continually scrolls on the screen with finally there[/color]
Yes, because you have no input command in the loop and no sum changing
the value of nu3.
But you should get scrolling with "Not there yet, next number please"
not with "Finally there!" according to your code.
I suppose your next question will be what tutorial is the best for a
newbie, so I suggest you start with: http://wiki.python.org/moin/BeginnersGuide
Claudio | | | | re: Beginner Programmer Question kydavis77@gmail.com wrote:[color=blue]
> I am doing alot of reading and trying to teach myself how to program.
> I can not figure out how to make "Write a program that continually
> reads in numbers from the user and adds them together until the sum
> reaches 100." this work. If someone could show me the correct code so i
> can learn from that it would be much appreciated. Thanks
>[/color]
sum = 0
while sum <= 100:
number = int(raw_input("Enter a number\n"))
sum = sum + number
print "Finally there"
As long as the sum is <= 100 the program asks for a number and adds it
to the sum variable. If the variable is >= 100, it exits and outputs the
string. | | | | re: Beginner Programmer Question
> bigone = 100[color=blue]
>
> number = input("Whats the first number?")
> number2 = input ("Whats the second number?")
> nu3 = number+number2
> while nu3 < bigone:
> print ("Not there yet, next number please")
>
> print "Finally there!"
>
> thats what i thought maybe it was...but after the first two numbers it
> just continually scrolls on the screen with finally there[/color]
Gaak! it's doing exactly what you told it to do! Bad program! :)
Inside the loop it neither
1) gets further input from the user nor
2) reassigns/sums nu3
Thus, neither of the elements of your condition (nu3 < bigone)
change, so you're stuck in an infinite loop of printing "not
there yet..."
Here's some sample code that works and does about what you
describe. Tweak accordingly. :)
total = 0
target = 3600
values = [
82, 69, 87, 83, 78, 65, 0, 89, 83, 85, 79, 76, 0, 83, 73,
72, 84, 0, 83, 65, 87, 0, 84, 79, 71, 0, 41, 0, 76, 76,
65, 0, 68, 78, 65, 0, 78, 79, 72, 84, 89, 80, 14, 71, 78,
65, 76, 14, 80, 77, 79, 67, 0, 78, 79, 0, 78, 79, 73, 84,
83, 69, 85, 81, 0, 75, 82, 79, 87, 69, 77, 79, 72, 0, 65,
0, 68, 69, 75, 83, 65, 0, 41
]
while total < target:
# still haven't found the value
value = values.pop() # so we get another number
print chr(value+32), #h have a little fun
total += value #accumulate our total
# and do it until we've accumlated more than target
print "Hey...we're over %s" % target
-tkc | | | | re: Beginner Programmer Question
Tim Chase wrote:[color=blue][color=green]
> > bigone = 100
> >
> > number = input("Whats the first number?")
> > number2 = input ("Whats the second number?")
> > nu3 = number+number2
> > while nu3 < bigone:
> > print ("Not there yet, next number please")
> >
> > print "Finally there!"
> >
> > thats what i thought maybe it was...but after the first two numbers it
> > just continually scrolls on the screen with finally there[/color]
>
> Gaak! it's doing exactly what you told it to do! Bad program! :)
>
> Inside the loop it neither
> 1) gets further input from the user nor
> 2) reassigns/sums nu3
>
> Thus, neither of the elements of your condition (nu3 < bigone)
> change, so you're stuck in an infinite loop of printing "not
> there yet..."
>
> Here's some sample code that works and does about what you
> describe. Tweak accordingly. :)
>
> total = 0
> target = 3600
> values = [
> 82, 69, 87, 83, 78, 65, 0, 89, 83, 85, 79, 76, 0, 83, 73,
> 72, 84, 0, 83, 65, 87, 0, 84, 79, 71, 0, 41, 0, 76, 76,
> 65, 0, 68, 78, 65, 0, 78, 79, 72, 84, 89, 80, 14, 71, 78,
> 65, 76, 14, 80, 77, 79, 67, 0, 78, 79, 0, 78, 79, 73, 84,
> 83, 69, 85, 81, 0, 75, 82, 79, 87, 69, 77, 79, 72, 0, 65,
> 0, 68, 69, 75, 83, 65, 0, 41
> ]
>
> while total < target:
> # still haven't found the value
> value = values.pop() # so we get another number
> print chr(value+32), #h have a little fun
> total += value #accumulate our total
> # and do it until we've accumlated more than target
> print "Hey...we're over %s" % target
>
>
> -tkc[/color]
I finally got it to work. Thanks all for the help. Im sure ill be back
with plenty more question!!! | | | | re: Beginner Programmer Question
<kydavis77@gmail.com> wrote in message
news:1151344078.584742.95990@y41g2000cwy.googlegro ups.com...[color=blue]
>
> whats the difference between raw input and input?[/color]
*That* you should look up in the builtin functions section of chapter 1? of
the library reference manual. Chapter 2 of the same on builtin types is
also important to skim and be able to refer back to. | | | | re: Beginner Programmer Question
On Mon, 26 Jun 2006 10:47:58 -0700, kydavis77@gmail.com wrote:[color=blue]
> whats the difference between raw input and input?[/color]
'input' is used to ask the user to input a python expression, which is
then run, and 'input' returns the result of executing that expression. For
example:
(define a function which prints something out)
[color=blue][color=green][color=darkred]
>>> def func():[/color][/color][/color]
.... print "You're calling a function"
.... return "Function result"
....
(now, call input)
[color=blue][color=green][color=darkred]
>>> res = input('Type python here: ')[/color][/color][/color]
(python outputs the prompt )
Type python here:
(and, if you type the python code to call a function after the prompt)
Type python here: func()
(then python calls the function, which prints out)
You're calling a function
(meanwhile, the result of calling the function gets assigned to 'res')
[color=blue][color=green][color=darkred]
>>> print res[/color][/color][/color]
Function result
'raw_input' just returns whatever text the user typed in. That's what you
want to use here. 'raw_input' returns a string, which you'll need to
convert to a number with the 'int' function, like so:
text = raw_input('Type in a number: ')
number = int(text) |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|