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

27th Dec 2023, 5:07 PM
Hanin Hanin
Hanin Hanin - avatar
9 odpowiedzi
+ 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.
27th Dec 2023, 6:44 PM
Lothar
Lothar - avatar
+ 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)
27th Dec 2023, 7:01 PM
JaScript
JaScript - avatar
+ 2
Graph is an empty array and as such index I doesn't exist in the array
27th Dec 2023, 5:25 PM
Aweneg Rooney
Aweneg Rooney - avatar
+ 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.
27th Dec 2023, 7:39 PM
Rain
Rain - avatar
+ 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())
29th Dec 2023, 7:22 AM
Mustufa Bahelim
Mustufa Bahelim - avatar
0
Hanin Hanin , If all you really want to do is convert str to int, the first line already did it.
27th Dec 2023, 8:32 PM
Rain
Rain - avatar
0
Thank you for your help thank you everyone.
29th Dec 2023, 1:46 PM
Hanin Hanin
Hanin Hanin - avatar
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.
29th Dec 2023, 1:49 PM
Rain
Rain - avatar