Beginner Programmer Question

K

kydavis77

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
 
C

Claudio Grondi

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
 
K

kydavis77

Claudio said:
Isn't it your homework?
Why can't you figure it out?
What have you tried?

Claudio

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
 
K

kydavis77

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

#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
 
R

Rune Strand

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.

;-)
 
S

SuperHik

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

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..."
 
S

SuperHik

Rune said:
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.

;-)
I don't think you understood his problem. He is trying to learn how to
*program*, not just learn Python ;)
 
K

kydavis77

Rune said:
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.

;-)

thanks for the help..but i am extremley new and what you said makes no
sense to me
 
R

Rune Strand

Rune said:
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.

;-)

thanks for the help..but i am extremley new and what you said makes no
sense to me

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 ;-)
 
K

kydavis77

Rune said:
Rune said:
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.

;-)

thanks for the help..but i am extremley new and what you said makes no
sense to me

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 ;-)

whats the difference between raw input and input?
 
M

Michael Goettsche

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

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.
 
C

Claudio Grondi

#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:
# you are missing the input and building the sum here:
number3 = input ("Whats the next number?")
nu3 = nu3 + number3
print ("Not there yet, next number please")
# so nu3 gets never changed and your loop runs forever.
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
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
 
T

Tim Chase

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

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
 
K

kydavis77

Tim said:
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

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

I finally got it to work. Thanks all for the help. Im sure ill be back
with plenty more question!!!
 
T

Terry Reedy

whats the difference between raw input and input?

*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.
 
I

I V

whats the difference between raw input and input?

'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)
.... print "You're calling a function"
.... return "Function result"
....

(now, call input)

(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')
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top