[Tutor] How do I fix this?
Alan Gauld
alan.gauld at btinternet.com
Wed Aug 15 00:49:19 CEST 2012
On 14/08/12 23:30, Lily Tran wrote:
> I have 320 dollars to buy grocery. My program would print out: I have
> 321 to buy grocery. However, the code that I have listed below would
> print out: I have 431 dollars to buy grocery. How do I fix this program
> so that it print out 321 vs. 431?
Since this is obviously a homework or tutorial exercise I'll only give
some hints.
> def WhereAreNumbers(a_list):
>
> for index, element in enumerate(a_list):
note: a_list is really a string not a list plus you aren't using index
so you could just use:
for char in a_string
> if element.isdigit():
> new_number = int(element)
You stop with the first digit but you really need to collect all the
digits before converting to a number. There are several ways to do this
but I'll discuss the most simplistic...
You need to store the digit you found into a list then fetch the next
character from the string and see if it too is a number, if so add it to
the list of current digits. When you get a non-digit convert your list
of digits to a number and add one.
So how to fetch the next char? Just let the loop "continue"(hint)
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list