473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jun 26 '06 #1
15 2098
ky*******@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
Jun 26 '06 #2

Claudio Grondi wrote:
ky*******@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


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

Jun 26 '06 #3

ky*******@gmail.com wrote:
Claudio Grondi wrote:
ky*******@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


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

Jun 26 '06 #4
>
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.

;-)

Jun 26 '06 #5

Rune Strand wrote:

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

Jun 26 '06 #6
ky*******@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


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..."
Jun 26 '06 #7
Rune Strand wrote:
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 ;)
Jun 26 '06 #8

ky*******@gmail.com wrote:
Rune Strand wrote:

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

Jun 26 '06 #9

Rune Strand wrote:
ky*******@gmail.com wrote:
Rune Strand wrote:
>
> 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?

Jun 26 '06 #10
ky*******@gmail.com wrote:
ky*******@gmail.com wrote:
Claudio Grondi wrote:
ky*******@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
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:

# 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
Jun 26 '06 #11
ky*******@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


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.

Jun 26 '06 #12
> 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


Jun 26 '06 #13

Tim Chase wrote:
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!!!

Jun 26 '06 #14

<ky*******@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...

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.

Jun 26 '06 #15
I V
On Mon, 26 Jun 2006 10:47:58 -0700, ky*******@gmail.com wrote:
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)
def func(): .... print "You're calling a function"
.... return "Function result"
....

(now, call input)
res = input('Type python here: ')
(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')
print 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)


Jun 26 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
14
by: japin0 | last post by:
Hi, I am a absolute beginner in ASP ..I do have a bit of exposure to .VBS and other scripting languages. I need your guidance, where do I start? I had been to MS website Quick start guide but it...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
7
by: Jeremy | last post by:
hello, I am trying to calculate the standard deviation with my program, but I ran into a small problem with squaring negative value's. Which cannot be done.. Ehm let me explain let's say; ...
5
by: Jacky Luk | last post by:
import java.awt.Graphics; public class printtest extends java.applet.Applet { public void init() { } public void paint (Graphics g) {
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
9
by: Daniel | last post by:
Looking to see if anyone can offer some suggestions on some good VB.net books? looking for beginner to intermidiate and to expert.. Any suggestions? -- ASP, SQL2005, DW8 VBScript
4
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.