Int as string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Int as string

def IntAsString(): num = [0, 1, 2, 3, 4] 0=zero 1= one 2=two 3=three 4=four how to take a int value between 0 to 4 & print as string?

7th Feb 2017, 6:03 PM
Efraim Ié
Efraim Ié - avatar
2 Answers
+ 3
def IntAsString(input): num = ["zero", "one", "two", "three", "four"] return num[input] print(IntAsString(3))
7th Feb 2017, 6:08 PM
Mario L.
Mario L. - avatar
+ 2
There are a couple of mistakes in your code. 1) You cannot assign something to a constant (0, 1, 2 ,3, etc) 2) Strings are surrounded with quotes ('zero', 'one', 'two', 'three', etc) The correct approach would be to use a dictionary. You can find more about a dictionary here: https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_dictionary.htm The Solution: https://repl.it/Fa81/1 The Code: def IntAsString(x): dict = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four' } if x in dict: print(dict[x]) else: print('Key not found') IntAsString(0) IntAsString(1) IntAsString(2) IntAsString(3) IntAsString(4) IntAsString(5)
7th Feb 2017, 6:18 PM
Filip Zirbo
Filip Zirbo - avatar