Help

K

khaosyt

If I wanted to get the sum of some numbers (for example: 1 + 2 + 3 + 4 + 5 = 15) from the attached program what do I do? Keep in mind that the print statement prints the integers individually.

integer = 0
denom = 10
again = "y" #sentinel:
while again == "y" or again == "Y":
integer = input("Enter a positive integer: ")
while denom <= integer:
denom = denom*10
while denom > 1:
denom = denom/10
number = integer/denom
integer = integer%denom
print str(number)
again = raw_input("Again? (Y/N): ")
 
M

Mark Lawrence

Self-bump

It's considered polite to wait for at least 24 hours before bumping a
question. You might have got more answers if you'd given a decent
subject line rather than "Help".

As it happens all you need do is loop around the string that is input,
converting every digit to an integer and storing them in a list, then
call the builtin sum function against the stored list.

You'll probably also need a try/except to handle any duff input.
 
K

khaosyt

It's considered polite to wait for at least 24 hours before bumping a

question. You might have got more answers if you'd given a decent

subject line rather than "Help".



As it happens all you need do is loop around the string that is input,

converting every digit to an integer and storing them in a list, then

call the builtin sum function against the stored list.



You'll probably also need a try/except to handle any duff input.



--

If you're using GoogleCrap™ please read this

http://wiki.python.org/moin/GoogleGroupsPython.



Mark Lawrence

How do I go about storing them in a list?
 
K

khaosyt

It's considered polite to wait for at least 24 hours before bumping a

question. You might have got more answers if you'd given a decent

subject line rather than "Help".



As it happens all you need do is loop around the string that is input,

converting every digit to an integer and storing them in a list, then

call the builtin sum function against the stored list.



You'll probably also need a try/except to handle any duff input.



--

If you're using GoogleCrap™ please read this

http://wiki.python.org/moin/GoogleGroupsPython.



Mark Lawrence

How do I go about storing them in a list?
 
D

Dave Angel

Self-bump

Normally, bumping a message/thread means replying to it. You're leaving
a NEW message with no context, no reply, and a different uninformative
subject line. And you're leaving it from googlegroups, with two copies.

If you want to sum things through a loop, initialize a variable to zero
before the loop, then add to it each time through the loop.

total=0
for whatever in something:
total += whatever_expression

print total
 
S

Steven D'Aprano

It's considered polite to wait for at least 24 hours before bumping a
question.

Yes, but the assignment is due in an hour. Therefore it's critical that
we reply instantly.

*wink*
 
R

Roy Smith

Steven D'Aprano said:
Yes, but the assignment is due in an hour. Therefore it's critical that
we reply instantly.

How do you know it's an assignment? It could be an interview.
 
M

Mark Lawrence

On Mon, 01 Apr 2013 15:12:20 -0700, khaosyt wrote:

<snip triple spaced homework rubbish double posted>

Sigh. Another one for the bozo bin.

I say old chap you're setting yourself up for attacks from the Python
Mailing List Police for using the word bozo, so expect a visit from
Vicar Sergeant or Detective Parsons. Oh sorry they're from the Church
Police, but please be cautious anyway.
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top