Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42441 > unrolled thread
| Started by | khaosyt@gmail.com |
|---|---|
| First post | 2013-03-31 22:15 -0700 |
| Last post | 2013-03-31 22:41 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
Help please khaosyt@gmail.com - 2013-03-31 22:15 -0700
Re: Help please Chris Angelico <rosuav@gmail.com> - 2013-04-01 16:24 +1100
Re: Help please khaosyt@gmail.com - 2013-03-31 22:41 -0700
Re: Help please Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-02 00:09 +0000
Re: Help please khaosyt@gmail.com - 2013-03-31 22:41 -0700
| From | khaosyt@gmail.com |
|---|---|
| Date | 2013-03-31 22:15 -0700 |
| Subject | Help please |
| Message-ID | <61cf6de1-179e-4d3d-8083-a2f34b144cd1@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-04-01 16:24 +1100 |
| Message-ID | <mailman.4046.1364793902.2939.python-list@python.org> |
| In reply to | #42441 |
On Mon, Apr 1, 2013 at 4:15 PM, <khaosyt@gmail.com> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | khaosyt@gmail.com |
|---|---|
| Date | 2013-03-31 22:41 -0700 |
| Message-ID | <d5945487-b1b9-480e-bbb8-5913e7449575@googlegroups.com> |
| In reply to | #42442 |
On Monday, April 1, 2013 1:24:52 AM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 4:15 PM, <khaosyt@gmail.com> wrote:
>
> > 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
Elaborate, please.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-04-02 00:09 +0000 |
| Message-ID | <515a21c6$0$29967$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #42444 |
On Sun, 31 Mar 2013 22:41:32 -0700, khaosyt wrote:
> On Monday, April 1, 2013 1:24:52 AM UTC-4, Chris Angelico wrote:
>> On Mon, Apr 1, 2013 at 4:15 PM, <khaosyt@gmail.com> wrote:
>>
>> > 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(...))
[...]
> 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.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | khaosyt@gmail.com |
|---|---|
| Date | 2013-03-31 22:41 -0700 |
| Message-ID | <mailman.4047.1364794901.2939.python-list@python.org> |
| In reply to | #42442 |
On Monday, April 1, 2013 1:24:52 AM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 4:15 PM, <khaosyt@gmail.com> wrote:
>
> > 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
Elaborate, please.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web