[Tutor] Adding numbers within a string

D.V.N.Sarma డి.వి.ఎన్.శర్మ dvnsarma at gmail.com
Wed Oct 12 12:50:59 EDT 2016


def string_sum(s):
      total = 0
      items = s.split(" ")
      for item in items:
           total += int(item)
      print(total)

You can call the function as

string_sum("10 4 5 ")

Output will be 19.
regards,
Sarma.


On Wed, Oct 12, 2016 at 3:22 PM, hell gates <hellgatez1 at yandex.com> wrote:
>    You can write your own str.split or just use while loop.
>    12.10.2016, 15:12, "LQ Soh" <sohlq710 at gmail.com>:
>
>      To whom it may concern,
>      Can someone enlighten me as to how you can create a function such
>      that sum_numbers('10 5 8'), when run, will give an answer of 23, without
>      using str.split() and using a for loop
>      _______________________________________________
>      Tutor maillist - [1]Tutor at python.org
>      To unsubscribe or change subscription options:
>      [2]https://mail.python.org/mailman/listinfo/tutor
>
> References
>
>    Visible links
>    1. mailto:Tutor at python.org
>    2. https://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list