Help please

K

khaosyt

I want to add up the integers of this code in one line. For example, if I had the code

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): ")

and inputted "54321," it would look like:
Enter a positive integer: 54321
5
4
3
2
1
Again? (Y/N): n

What I want to do is add up the "54321" so it comes out with
"Sum of digits is 15." on one line.

Thanks!
 
C

Chris Angelico

integer = input("Enter a positive integer: ")
again = raw_input("Again? (Y/N): ")

Okay, the first thing I'm going to say is: Don't use input() in Python
2. It's dangerous in ways you won't realize. Use int(raw_input(...))
for something like this, which will guarantee you an integer.

I'm guessing this is homework. Please be honest about that; we'll help
you learn but won't just give you the answers.

All you need to do is initialize something to zero, and then keep
adding 'number' onto it in the loop. You should be able to sort that
out.

ChrisA
 
K

khaosyt

Okay, the first thing I'm going to say is: Don't use input() in Python

2. It's dangerous in ways you won't realize. Use int(raw_input(...))

for something like this, which will guarantee you an integer.



I'm guessing this is homework. Please be honest about that; we'll help

you learn but won't just give you the answers.



All you need to do is initialize something to zero, and then keep

adding 'number' onto it in the loop. You should be able to sort that

out.



ChrisA

Elaborate, please.
 
K

khaosyt

Okay, the first thing I'm going to say is: Don't use input() in Python

2. It's dangerous in ways you won't realize. Use int(raw_input(...))

for something like this, which will guarantee you an integer.



I'm guessing this is homework. Please be honest about that; we'll help

you learn but won't just give you the answers.



All you need to do is initialize something to zero, and then keep

adding 'number' onto it in the loop. You should be able to sort that

out.



ChrisA

Elaborate, please.
 
S

Steven D'Aprano

Okay, the first thing I'm going to say is: Don't use input() in Python

2. It's dangerous in ways you won't realize. Use int(raw_input(...))
[...]

Elaborate, please.


The input() function takes the user's text, and automatically evaluates
it as if it were code. In the hands of a non-expert, that can lead to
some unexpected errors:

py> answer = input("what is your name? ")
what is your name? Steve
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Steve' is not defined



But at least that gives you an error. It can also give you weird and
unexpected results. Suppose my name was Lenny, and I did this:

py> answer = input("what is your name? ")
what is your name? len
py> print answer
<built-in function len>



Worse, because input evaluates text as code, it can do anything,
including bad things:

py> answer = input("what is your name? ")
what is your name? 200**300**300
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
MemoryError


(while the above code was running, my computer got slower and slower and
slower, and potentially it could have locked up completely).


So the general advice is, treat the input() function as For Experts Only,
and always use raw_input() instead.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top