0

I am trying to make a program in python that takes an index value and outputs a string based on the input value; then I need it to respond in english. For example, index 0 = value 0 One zero., index 1 = value 10 One one. One zero., index 2 = value 1110 Three ones. One zero. , index 3 = value 3110 One three. Two ones. One zero., etc.

initialIndexGlobal = "0"

nextIndexGlobal = ""

string = initialIndexGlobal

def stringHead():
    initialIndexGlobal[0]

def StringTail():
    initialIndexGlobal[1:]

def checkValueIterative(string):
    nextIndex = ""
    string = string + "4"
    initialValue = string[0]
    counter = 0

    for w in string:
        if w == initialValue:
            counter = counter + 1
        else:
            secondDigit = str(initialValue)
            firstDigit = str(counter)
            #print(firstDigit + secondDigit)
            global nextIndexGlobal
            nextIndexGlobal = nextIndexGlobal + str(firstDigit) + str(secondDigit)
            string = string[(counter):]
            checkValueIterative(string)
            break
    return(nextIndex)

def checkValueRecursion(index):
    if index == 0:
        print(initialIndexGlobal)
    else:
        checkValueIterative(initialIndexGlobal)
        global initialIndexGlobal
        global nextIndexGlobal
        initialIndexGlobal = nextIndexGlobal
        nextIndexGlobal = ""
        checkValueRecursion(index-1)

def runLen(string, index):
    if string == '':
        return 0
    if stringHead()==index:
        return 1 + runLen(StringTail, index)
    else:
        return 0


def say(string):
    if string == "":
        return
    else:
        if ( string[ 0 ] )== "0" and runLen( string,string[ 0 ]==3 ):
            print( "three zeros" )
            return say( string[ 3:1 ] )

        elif (string[ 0 ] == "0" and runLen( string,string[ 0 ]==2 ) ):
            print( "two zeros" )
            return say( string[ 2:1 ] )

        elif ( string[ 0 ]=="0" ):
            print( "one zero" )
            return say( string )

        if (string[ 0 ] == "1" and runLen( string,string[ 0 ]==3 ) ):
            print( "three ones" )

            return say( string[ 3:1 ] )
        elif (string[ 0 ] == "1" and runLen( string,string[ 0 ]==2 ) ):
            print( "two ones" )
            return say( string[ 2:1 ] )

        elif (string[ 0 ] == "1" ):
            print( "one one" )
            return say( string )

        if (string[ 0 ] == "2" and runLen( string,string[ 0 ]==3 ) ):
            print( "three twos" )
            return say( string[ 3:1 ] )

        elif (string[ 0 ] == "2" and runLen( string,string[ 0 ]==2 ) ):
            print( "two twos" )
            return say( string[ 2:1 ] )

        elif (string[ 0 ]=="2" ):
            print( "one two" )
            return say( string )

        if (string[ 0 ]=="3"and runLen( string,string[ 0 ]==3 ) ):
            print( "three threes" )
            return say( string[ 3:1 ] )

        elif (string[ 0 ]=="3"and runLen( string,string[ 0 ]==2 ) ):
            print( "two Threes" )
            return say( string[ 2:1 ] )

        elif (string[ 0 ]=="3" ):
            print( "one three" )
            return say( string )



#index = input("Enter Index value here: ")
checkValueRecursion(10)
runLen(string, initialIndexGlobal)
say(string)
input()

This is what I have so far. It calculates the index value correctly, but it does not repeat it in english; instead it re-curses until it crashes. How would I go about fixing this?

4
  • First, that final block of ifs and elifs is a nightmare. Rewrite that with some spacing and nest it so it's easier to understand. Second, you've got (at least) one significant typo, the last return has the variable misspelled. Commented Oct 1, 2013 at 19:45
  • I fixed the spacing of the ifs and elifs and I fixed the typos. What do you mean nesting? Commented Oct 1, 2013 at 20:14
  • within the say(string) function, you are saying return say(X) so it just calls itself again. You want to return X... (although I'm not convinced that X of string[2:1] etc. is defined correctly... What are you trying to get with the [3:1] slicing? Commented Oct 1, 2013 at 21:39
  • I think that we're just trying to cut out that part of the string so that the function can read it. Commented Oct 1, 2013 at 22:35

2 Answers 2

1

Ok, we figured it out. We completely removed the old say() completely and rewrote it. Thanks for the responses.

initialIndexGlobal = "0"
nextIndexGlobal = ""
stringComposition = ""

def numberToLetter(string):
    numtotext = ""
    for w in string:
        if w == "0":
            numtotext = numtotext + "zero "
        elif w == "1":
            numtotext = numtotext + "one "
        elif w == "2":
            numtotext = numtotext + "two "
        else:
            numtotext = numtotext + "three "
    return numtotext

def checkValueIterative(string):
    nextIndex = ""
    string = string + "4"
    initialValue = string[0]
    counter = 0
    for w in string:
        if w == initialValue:
            counter = counter + 1
        else:
            secondDigit = str(initialValue)
            firstDigit = str(counter)
            #print(firstDigit + secondDigit)
            global nextIndexGlobal
            nextIndexGlobal = nextIndexGlobal + str(firstDigit) + str(secondDigit)
            string = string[(counter):]
            checkValueIterative(string)
            break
    return(nextIndex)

def checkValueRecursion(index):
    if index == 0:
        return
    else:
        checkValueIterative(initialIndexGlobal)
        global initialIndexGlobal
        global nextIndexGlobal
        initialIndexGlobal = nextIndexGlobal
        nextIndexGlobal = ""
        checkValueRecursion(index-1)

def main():
    indexValue=int(input("What is the index value?"))
    checkValueRecursion(indexValue)
    print(str(indexValue) + " : " + str(initialIndexGlobal) + " : " + str(numberToLetter(initialIndexGlobal)))
main()
Sign up to request clarification or add additional context in comments.

Comments

0

say will loop infinitely on the string '0'

say("0")
# runLen("0",False) and runLen("0",True) won't return 2 or 3
# We move on to the line:
    if string[ 0 ]=="0": #this passes because it is true.
        print('one zero')
        say(string)

As a note, you have a lot of runLen( string,string[ 0 ]==3 ), I think you really mean runLen( string,string[ 0 ] ) ==3. Also, string = initialIndexGlobal should probably be removed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.