def int_to_str[num]:
is_negative = False
if num:
num, is_negative = -num, True
s = []
while True:
s.append[chr[ord['0'] + num % 10]]
num //= 10
if num == 0:
break
return ['-' if is_negative else ''] + ''.join[reversed[s]]
num = input["Enter any numeric value: "]
print[int_to_str[num]]
ayhan
66.1k17 gold badges173 silver badges191 bronze badges
asked Jun 11, 2017 at 21:39
6
If you have only an integer in the conditional of an if statement, the if statement will run if and only if the integer is not equal to zero. You need to do
if num < 0:
not
if num:
But indeed, @user8145959 has a point. The number inputted is already a string. When you pass the input string to int_to_str, it gets automatically converted to an integer at the places where you try integer operations on it.
Edit: Automatic integer conversion works only in python 2, not python 3. My mistake.
Edit2: Actually I'm just wrong. Didn't realize input[]
did the conversion already.
answered Jun 11, 2017 at 21:47
xunataixunatai
1711 silver badge6 bronze badges
4
The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57.
num = 56
s = [0 if not num else '']
while num:
s.append[chr[48 + num % 10]]
num = num // 10
a= "".join[reversed[s]]
print[a]
print[type[a]]
output->
56
answered Dec 10, 2020 at 9:38
NIMINIMI
738 bronze badges
You can use the chr[] and ord[] just like you did:
def int_to_str[input_int]:
if input_int < 0:
is_negative = True
input_int *= -1
else:
is_negative = False
output_str = []
while input_int > 0:
# print[input_int % 10]
output_str.append[chr[ord["0"] + input_int % 10]]
input_int //= 10
output_str = "".join[output_str[::-1]]
if is_negative:
return "-" + output_str
else:
return output_str
print[int_to_str[123]]
print[int_to_str[-123]]
>>> "123"
>>> "-123"
rel
7265 silver badges18 bronze badges
answered Aug 13, 2021 at 14:56
Python takes input as a string So you do not need to write a function for it When you take input num It is already a string
answered Jun 11, 2017 at 21:44
2
Your function doesn't work because you're doing a couple of things wrong. First, it will crash on the tagged python-3.x
as it doesn't evaluate the input so you're essentially passing a string to your function as input[]
returns a string. You should use:
num = int[input["Enter any numeric value: "]] # and you should use raw_input[] on Python 2.x
instead. You may want to validate the input first, too, to make sure that the integer was selected. Second, it always declares your num
as negative as if num:
will always return True for every value other than
0
, so use if num < 0:
instead.
Also, you're adding more conditions to it than needed, you can use your value in the while
statement directly, i.e.:
s = [0 if not num else '']
while num:
s.append[chr[48 + num % 10]]
num //= 10
answered Jun 11, 2017 at 22:05
zwerzwer
24k3 gold badges42 silver badges59 bronze badges
def int_to_str[num]:
s = []
while True:
digit = num % 10
num = num // 10
if digit == num: # or we could also use digit == 0 condition
break
s.append[chr[ord['0'] + digit]]
return ''.join[s[::-1]]
answered Oct 3, 2021 at 6:16
1
Solution using recursion.
def integer_to_string[i]:
# Edge case
if i == 0:
return chr[i+48]
# Handling negative number
negative_integer = False
if i < 0:
negative_integer = True
i = - i
def convert_to_string[i]:
if i < 10:
return chr[i+48]
else:
return [convert_to_string[i // 10]] + chr[[i%10]+48]
result = convert_to_string[i]
if negative_integer:
return '-' + result
else:
return result
answered Nov 17, 2021 at 7:17
This will work for you
def int_to_str[num]:
is_negative = False
if num:
num, is_negative = num, True
s = []
while True:
s.append[chr[ord['0'] + num % 10]]
num //= 10
if num == 0:
break
return ''.join[reversed[s]]
answered Aug 28, 2018 at 15:36
1