Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42508 > unrolled thread

Help

Started bykhaosyt@gmail.com
First post2013-04-01 14:44 -0700
Last post2013-04-01 17:38 -0700
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Help khaosyt@gmail.com - 2013-04-01 14:44 -0700
    Re: Help John Gordon <gordon@panix.com> - 2013-04-01 22:12 +0000
      Re: Help khaosyt@gmail.com - 2013-04-01 15:22 -0700
        Re: Help John Gordon <gordon@panix.com> - 2013-04-01 22:33 +0000
          Re: Help khaosyt@gmail.com - 2013-04-01 15:57 -0700
    Re: Help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-02 00:00 +0000
      Re: Help khaosyt@gmail.com - 2013-04-01 17:38 -0700

#42508 — Help

Fromkhaosyt@gmail.com
Date2013-04-01 14:44 -0700
SubjectHelp
Message-ID<fb1d2908-e0be-461f-a8ef-eb4903c8bb7a@googlegroups.com>
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): ") 

[toc] | [next] | [standalone]


#42513

FromJohn Gordon <gordon@panix.com>
Date2013-04-01 22:12 +0000
Message-ID<kjd0or$hp5$1@reader1.panix.com>
In reply to#42508
In <fb1d2908-e0be-461f-a8ef-eb4903c8bb7a@googlegroups.com> khaosyt@gmail.com writes:

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

It's not clear what you're asking for.  The attached program doesn't
(appear to) work with sums at all; why would you want to use it?
Writing a new program from scratch would seem to be a better choice.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#42514

Fromkhaosyt@gmail.com
Date2013-04-01 15:22 -0700
Message-ID<118e161a-4981-4696-8bdb-780bf82484cc@googlegroups.com>
In reply to#42513
On Monday, April 1, 2013 6:12:43 PM UTC-4, John Gordon wrote:
> In <fb1d2908-e0be-461f-a8ef-eb4903c8bb7a@googlegroups.com> khaosyt@gmail.com writes:
> 
> 
> 
> > 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. 
> 
> 
> 
> It's not clear what you're asking for.  The attached program doesn't
> 
> (appear to) work with sums at all; why would you want to use it?
> 
> Writing a new program from scratch would seem to be a better choice.
> 
> 
> 
> -- 
> 
> John Gordon                   A is for Amy, who fell down the stairs
> 
> gordon@panix.com              B is for Basil, assaulted by bears
> 
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

It doesn't work with sums yet; it only returns individual positive integers in a longer number. 

[toc] | [prev] | [next] | [standalone]


#42517

FromJohn Gordon <gordon@panix.com>
Date2013-04-01 22:33 +0000
Message-ID<kjd1v4$k9c$1@reader1.panix.com>
In reply to#42514
In <118e161a-4981-4696-8bdb-780bf82484cc@googlegroups.com> khaosyt@gmail.com writes:

> > It's not clear what you're asking for.  The attached program doesn't
> > (appear to) work with sums at all; why would you want to use it?
> > Writing a new program from scratch would seem to be a better choice.

> It doesn't work with sums yet; it only returns individual positive
> integers in a longer number. 

Yes, I can see that.  But your original request said that you wanted to
get a sum of numbers "from the attached program", which sort of implies
that you want to use the program exactly as it was posted, without
modifications.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#42521

Fromkhaosyt@gmail.com
Date2013-04-01 15:57 -0700
Message-ID<7c0a74a6-dd3b-457f-8eb3-ce0e8bccb415@googlegroups.com>
In reply to#42517
On Monday, April 1, 2013 6:33:08 PM UTC-4, John Gordon wrote:
> In <118e161a-4981-4696-8bdb-780bf82484cc@googlegroups.com> khaosyt@gmail.com writes:
> 
> 
> 
> > > It's not clear what you're asking for.  The attached program doesn't
> 
> > > (appear to) work with sums at all; why would you want to use it?
> 
> > > Writing a new program from scratch would seem to be a better choice.
> 
> 
> 
> > It doesn't work with sums yet; it only returns individual positive
> 
> > integers in a longer number. 
> 
> 
> 
> Yes, I can see that.  But your original request said that you wanted to
> 
> get a sum of numbers "from the attached program", which sort of implies
> 
> that you want to use the program exactly as it was posted, without
> 
> modifications.
> 
> 
> 
> -- 
> 
> John Gordon                   A is for Amy, who fell down the stairs
> 
> gordon@panix.com              B is for Basil, assaulted by bears
> 
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

I want to modify it so it can be used to find the sums.

[toc] | [prev] | [next] | [standalone]


#42529

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-02 00:00 +0000
Message-ID<515a1f9d$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#42508
On Mon, 01 Apr 2013 14:44:45 -0700, khaosyt wrote:

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


Yes, we know what the print statement does. Some of us have been using 
Python for weeks now.


Some comments interspersed within your code below:



> integer = 0
> denom = 10
> again = "y" #sentinel:

Technically, that's not a sentinel.

> while again == "y" or again == "Y":
>     integer = input("Enter a positive integer: ")

I believe that the last time you asked this question, you were told not 
to use the "input" function as it was dangerous or can lead to hard-to-
understand bugs. Change the above line to:

    integer = raw_input("Enter a positive integer: ")


Notice that if you do this, the so-called "integer" is actually a string. 
This is a good thing! You want it as a string, since that makes it easy 
to extract individual digits.


>     while denom <= integer:
>         denom = denom*10
>     while denom > 1:
>         denom = denom/10
>         number = integer/denom
>         integer = integer%denom
>         print str(number)

All this stuff with denom seems to be aimed at extracting the digits from 
a number. There's an easier way: just work with the string.

After the line I suggested above 

    integer = raw_input("Enter a positive integer: ")


"integer" is a string of digits. So you can iterate over the digits using 
a for-loop:

# this is not what you want!
for digit in integer:
    print digit


Instead of printing the digits, you want to add them up. So start by 
initialising a total, then add them:

    total = 0
    for digit in integer:
        total = total + digit


Warning! The above three lines contains a bug. If you make the changes I 
suggest, and try it, you will get an error. That's okay. Read the error. 
Try to understand what it is telling you. Hint: remember that total is an 
actual int, a number, while each digit is a single character, a string.

You need to convert each digit into a number before adding it. Hint: the 
int function takes a string, and converts it to a number.

py> 42 + "23"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
py> 42 + int("23")
65


This should hopefully give you enough information to get some working 
code. Try to write as much of the code as you can, and come back with any 
further questions *after* making a good effort.

Another hint: try experimenting at the interactive interpreter, or IDLE. 
If you're unsure about something, try it and see what happens *before* 
asking.


Good luck.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#42534

Fromkhaosyt@gmail.com
Date2013-04-01 17:38 -0700
Message-ID<0d182421-22cf-4886-8406-bf9150026bac@googlegroups.com>
In reply to#42529
On Monday, April 1, 2013 8:00:30 PM UTC-4, Steven D'Aprano wrote:
> On Mon, 01 Apr 2013 14:44:45 -0700, khaosyt wrote:
> 
> 
> 
> > 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.
> 
> 
> 
> 
> 
> Yes, we know what the print statement does. Some of us have been using 
> 
> Python for weeks now.
> 
> 
> 
> 
> 
> Some comments interspersed within your code below:
> 
> 
> 
> 
> 
> 
> 
> > integer = 0
> 
> > denom = 10
> 
> > again = "y" #sentinel:
> 
> 
> 
> Technically, that's not a sentinel.
> 
> 
> 
> > while again == "y" or again == "Y":
> 
> >     integer = input("Enter a positive integer: ")
> 
> 
> 
> I believe that the last time you asked this question, you were told not 
> 
> to use the "input" function as it was dangerous or can lead to hard-to-
> 
> understand bugs. Change the above line to:
> 
> 
> 
>     integer = raw_input("Enter a positive integer: ")
> 
> 
> 
> 
> 
> Notice that if you do this, the so-called "integer" is actually a string. 
> 
> This is a good thing! You want it as a string, since that makes it easy 
> 
> to extract individual digits.
> 
> 
> 
> 
> 
> >     while denom <= integer:
> 
> >         denom = denom*10
> 
> >     while denom > 1:
> 
> >         denom = denom/10
> 
> >         number = integer/denom
> 
> >         integer = integer%denom
> 
> >         print str(number)
> 
> 
> 
> All this stuff with denom seems to be aimed at extracting the digits from 
> 
> a number. There's an easier way: just work with the string.
> 
> 
> 
> After the line I suggested above 
> 
> 
> 
>     integer = raw_input("Enter a positive integer: ")
> 
> 
> 
> 
> 
> "integer" is a string of digits. So you can iterate over the digits using 
> 
> a for-loop:
> 
> 
> 
> # this is not what you want!
> 
> for digit in integer:
> 
>     print digit
> 
> 
> 
> 
> 
> Instead of printing the digits, you want to add them up. So start by 
> 
> initialising a total, then add them:
> 
> 
> 
>     total = 0
> 
>     for digit in integer:
> 
>         total = total + digit
> 
> 
> 
> 
> 
> Warning! The above three lines contains a bug. If you make the changes I 
> 
> suggest, and try it, you will get an error. That's okay. Read the error. 
> 
> Try to understand what it is telling you. Hint: remember that total is an 
> 
> actual int, a number, while each digit is a single character, a string.
> 
> 
> 
> You need to convert each digit into a number before adding it. Hint: the 
> 
> int function takes a string, and converts it to a number.
> 
> 
> 
> py> 42 + "23"
> 
> Traceback (most recent call last):
> 
>   File "<stdin>", line 1, in <module>
> 
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
> 
> py> 42 + int("23")
> 
> 65
> 
> 
> 
> 
> 
> This should hopefully give you enough information to get some working 
> 
> code. Try to write as much of the code as you can, and come back with any 
> 
> further questions *after* making a good effort.
> 
> 
> 
> Another hint: try experimenting at the interactive interpreter, or IDLE. 
> 
> If you're unsure about something, try it and see what happens *before* 
> 
> asking.
> 
> 
> 
> 
> 
> Good luck.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

Thanks. I'll try it out.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web