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?