0
Please help me complete this code in python.
I want to create a code to turn number to words, but the list is not working pls help. digit = [input("Enter a Number: ")] words = [] if digit[-1] == 0: words.append(" ") elif digit[-1] == 1: words.append("one") elif digit[-1] == 2: words.append("two") print(words)
5 Respostas
+ 6
input() return type is string ,if you want an integer number apply int method to cast it to that type ,
int(input ())
+ 4
First of all, why are you using the variable `digit` as a list? If you just keep it a string, you don't need to acces the -1th element each time you want to compare the input.
Secondly, as Abhay said, in the if conditions, you are comparing type `str` to `int`, which are not possible to compare. Instead compare digit[-1] with the string version of the numbers, like so
if digit[-1] == "0":
and so on, or follow Abhay's suggestion
+ 4
And another thing, For example I input 1234, the result list of "digit" variable would be like this:
["1234"], i.e. only one element.
Since integers cannot be converted into list like strings and unindexed. You should get your input as string but at the same time your comparisons should be also a string. For example:
x = input()
if x[-1] == "0":
+ 1
thank you everyone