I'm new to python and I required to write a program that includes converting numbers to strings without using any built in functions beside len()
and .index()
. What I want to do is convert the number into a list
of individual integers and iterate through it, finding the corresponding string for each number and inputting it into a new list, and finally piling it all into one string at the end. This is my program:
def convertToString(integer):
strList = []
numList = []
for x in integer:
numList = numList + [x]
if len(numList) == 0:
raise ValueError()
for char in numList:
string = ""
if char == 1:
string = "1"
elif char == 2:
string = "2"
elif char == 3:
string = "3"
elif char == 4:
string = "4"
elif char == 5:
string = "5"
elif char == 6:
string = "6"
elif char == 7:
string = "7"
elif char == 8:
string = "8"
elif char == 9:
string = "9"
elif char == 0:
string = "0"
else:
string = char
strList = strList+[string]
finalResult = ""
for x in strList:
finalResult = finalResult + [x]
return finalResult
The error tells me that: I cannot iterate through the digits of a float or integer. How can I solve this problem?