-1

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?

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
  • its not the way to do that. You need to use integer division and modular math to construct single digits from your big number, then you can test the single digits for being [0 to 9] and add the correct string into your list that you recombine later. – Patrick Artner Jul 12 '20 at 17:12

3 Answers3

3

A slightly verbose solution, using division and modulo operations:

def convertToString(integer):
    rv, nums = '', '0123456789'
    while True:
        n, r = integer // 10, integer % 10
        rv = nums[r] + rv
        if n == 0:
            break
        integer = n
    return rv

print( convertToString(10023) )

Prints string:

10023
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Divide the number by 10 and use modulus 10 to get each digit and pull the str form of the digit from a list.

numstrs = ["0","1","2","3","4","5","6","7","8","9"]

num = 22

s = ""
while num > 0:
  s += numstrs[ num%10 ]
  num = (int)(num / 10 )
print(s)
Solivi
  • 21
  • 5
0

The error is correct: you didn't try to iterate through the digits of the integer; you tried to iterate through the integer itself. The object of a for must be an iterable, as the documentation tells you. An integer is an atomic object. The decimal number you see on output is a human-readable representation of the integer, not the integer object.

You can iterate through a list, tuple, string, or any other sequence. Eventually, you will learn about other iterables. You have to do the arithmetic to duplicate what happens when someone asks to print an integer in its decimal form. Look up how to handle base-10 representation. You can learn some of this by looking up how to convert to any number base.

Prune
  • 76,765
  • 14
  • 60
  • 81