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

2 回答 2

1

好的,我们想通了。我们完全删除了旧的 say() 并重写了它。感谢您的回复。

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()
于 2013-10-01T23:02:44.917 回答
0

say将在字符串 '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)

作为一个说明,你有很多runLen( string,string[ 0 ]==3 ),我认为你的意思是runLen( string,string[ 0 ] ) ==3。此外,string = initialIndexGlobal可能应该被删除。

于 2013-10-01T21:46:41.037 回答