Re: [Tutor] Adding numbers within a string
Please do not give homework solutions. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
Then Isaac's function does that(but you have to leave a space after last number in the string). def foo(x): a, b = 0, "" for i in x: if i != " ": b += i else: print ("this is b", b) a += int(b) print ("this is a",a) b = "" print ("final a ",a) foo("10 5 8 ") regards, Sarma. On Wed, Oct 12, 2016 at 11:35 PM, Alan Gauld via Tutor wrote: > On 12/10/16 17:58, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: >> Sorry, I did not read the conditions that split and for should not be used. > > It is even more confusing - split() may NOT be used > but 'for' MUST be used... > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
You can write your own str.split or just use while loop. 12.10.2016, 15:12, "LQ Soh" : 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@python.org To unsubscribe or change subscription options: [2]https://mail.python.org/mailman/listinfo/tutor References Visible links 1. mailto:Tutor@python.org 2. https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
On 12/10/16 17:58, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Sorry, I did not read the conditions that split and for should not be used. It is even more confusing - split() may NOT be used but 'for' MUST be used... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
Sorry, I did not read the conditions that split and for should not be used. regards, Sarma. On Wed, Oct 12, 2016 at 10:20 PM, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > 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 wrote: >>You can write your own str.split or just use while loop. >>12.10.2016, 15:12, "LQ Soh" : >> >> 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@python.org >> To unsubscribe or change subscription options: >> [2]https://mail.python.org/mailman/listinfo/tutor >> >> References >> >>Visible links >>1. mailto:Tutor@python.org >>2. https://mail.python.org/mailman/listinfo/tutor >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
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 wrote: >You can write your own str.split or just use while loop. >12.10.2016, 15:12, "LQ Soh" : > > 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@python.org > To unsubscribe or change subscription options: > [2]https://mail.python.org/mailman/listinfo/tutor > > References > >Visible links >1. mailto:Tutor@python.org >2. https://mail.python.org/mailman/listinfo/tutor > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
def foo(x): a,b = (0,"") for i in x: if i != " ": b+=i else: print ("this is b",b) a = a + int(b) print ("this is a",a) b = "" print ("final a ",a) this should help you solve it. figure a way to add the last number. if any troubles ask again. Thanks [😊] From: Tutor on behalf of Bob Gailer Sent: Wednesday, October 12, 2016 2:26 PM To: LQ Soh Cc: tutor@python.org Subject: Re: [Tutor] Adding numbers within a string On Oct 12, 2016 4:09 AM, "LQ Soh" wrote: > > 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 def sum_numbers(x): for x in [1]: print(23) Your instructor will probably be displeased with that answer, but it meets the requirements. It would be nice if our teachers could models writing good requirements. What do you think a good requirement statement would be? ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Tutor Info Page - mail.python.org Mailing Lists<https://mail.python.org/mailman/listinfo/tutor> mail.python.org This list is for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Tutor Info Page - mail.python.org Mailing Lists<https://mail.python.org/mailman/listinfo/tutor> mail.python.org This list is for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
On Oct 12, 2016 4:09 AM, "LQ Soh" wrote: > > 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 def sum_numbers(x): for x in [1]: print(23) Your instructor will probably be displeased with that answer, but it meets the requirements. It would be nice if our teachers could models writing good requirements. What do you think a good requirement statement would be? ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding numbers within a string
On 12/10/16 01:37, LQ Soh wrote: > 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 I'm assuming this is some kind of classroom exercise? Otherwise the advice is just use string split()! If it is a class exercise then the easiest option is to write your own string split(). That is a function that takes in a string and spits out substrings. To do that you need to traverse the string and look for separators. Store the characters between separators in a list. return the list. Another way to do it is to use a regular expression to detect the groups of characters - do you know about regex? Once you have done the splitting you need to convert the substrings into numbers and add them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor