我正在尝试在python中创建一个程序,它接受一个索引值并根据输入值输出一个字符串;然后我需要它用英语回复。例如,索引0 =值0一个零。,索引1 =值10一个。一个零。,索引2 =值1110三个。一零。 ,指数3 =价值3110一三。两个。一零等等。
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()
这是我到目前为止所拥有的。它正确计算索引值,但不会用英文重复;相反,它重新诅咒直到它崩溃。我该如何解决这个问题?
答案 0 :(得分: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()
答案 1 :(得分: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
应该被删除。