Hi,
Is it not clear to me if you must distinguish ints from other type of
numbers, or if, for instances floats and ints must be dealt
differently.
Anyway, I would propose something like the following function:
def FindNumbers(a_string):
print "You entered:", a_string
out_list = []
for item in a_string.split():
try:
num = int(item)
except ValueError:
out_list.append(item)
else:
out_list.append("%s" % (num+a,))
out_string = ' '.join(out_list)
# do whatever you want to do with the resulting out_string:
return it, or display it...
Some comments:
1) I would pass the input string as argument rather than using it as a global.
2) You could use float instead of int to make it more general
3) If you need to distinguish between ints and floats, then you must
add a couple of extra lines
I hope it helps.
Best
2014-02-04 Colin Struthers <[email protected]>:
> I am in a beginning python course and am working through some code and I
> can't even figure out how to start building this particular section of code.
>
> My goal is to get a sentence for the user and to take each number in the
> user string and add 1 to each number. i.e "the 4 people had 6 dogs" would
> change to "the 5 people had 7 dogs"
>
> a_string = [ ]
> int_list = [ ]
> a_string = raw_input("Enter a sentence with both words and numbers: ")
>
> def FindNumbers():
> print "You entered: ", a_string
> for ints in a_string
> ...?
>
> FindNumbers()
>
> I fully understand that this doesn't even begin to work but I don't really
> know where to start deconstructing the list, editing only the intergers, and
> joining it all back together.
>
> Thanks for the help.
>
> --
> Colin
>
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor