[Tutor] How do I fix this?

Lily Tran lilytran at adobe.com
Wed Aug 15 01:06:47 CEST 2012


I figure out how to do this.

Here is the code that would allow me to print 320 to 321:


def WhereAreNumbers(a_list):

    numberString=''

    for index, element in enumerate(a_list):

       if element.isdigit():

           numberString += element

       else:

            if numberString != "":

                print(int(numberString)+1,end="")

                numberString = ""

            print(element,end="")

    if numberString != "":

        print(int(numberString)+1,end="")




def Test():

    answer = input("Please enter your strings:")

    if answer:

        WhereAreNumbers(answer)


==================


Thanks;


Lily

From: Lily Tran <lilytran at adobe.com<mailto:lilytran at adobe.com>>
Date: Tue, 14 Aug 2012 15:30:17 -0700
To: "tutor at python.org<mailto:tutor at python.org>" <tutor at python.org<mailto:tutor at python.org>>
Subject: How do I fix this?

I am trying to read  in a string from the user input and if the string is a number then I would print out the numbers + 1.  For example if the user enters:
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?

=====================================================

#!/usr/bin/python3



def WhereAreNumbers(a_list):

    for index, element in enumerate(a_list):

        if element.isdigit():

            new_number = int(element)

            print (new_number + 1, end="")

        else:

            print(element,end="")



def Test():

    answer = input("Please enter your strings:")

    if answer:

        WhereAreNumbers(answer)

Test()

==========================================

Please enter your strings:I have 320 dollars to buy grocery

I have 431 dollars to buy grocery


Thanks;


Lily
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120814/73dd026b/attachment.html>


More information about the Tutor mailing list