0
How convert string to int? I don't what is wrong with this code?
I have this code Number=int(input("give me the number")) Graph=[] For i in number: Graph[i]=i
9 ответов
+ 8
Hanin Hanin ,
there are several other issues in the presented code:
> there are 2 different variables (`Number` and `number`). i suppose it should be only one of them. since python style guide recomments lower case names for variables use `number`.
> `For` (in the loop header) has to be in lower case.
> the code is trying to iterate over an integer number which is not possible.
(TypeError: 'int' object is not iterable)
]> we have no task description and no idea what your code should going to achieve.
may be you can add the missing requirements.
+ 3
# As Lothar mentioned a task description is must have
# This can be a way to solve this question:
number=input("give me the number: ")
Graph=[]
for i in range(len(number)):
Graph.append(int(number[i]))
print(number)
print(Graph)
+ 2
Graph is an empty array and as such index I doesn't exist in the array
+ 2
Hanin Hanin ,
When posting code, make sure to copy and paste it from the playground rather than retyping it by hand, so any original errors are intact.
+ 1
Hanin Hanin Adding to above, input doesn't need string text, the code given will result in an iteration error. You can simply write
numbers = int(input())
0
Hanin Hanin ,
If all you really want to do is convert str to int, the first line already did it.
0
Thank you for your help thank you everyone.
0
Hanin Hanin ,
Cool. If you solved it, paste your final code (I solved it offline, but I'm curious to see how you did it too), then add [Solved] to the discussion title.