Reputation:
i am trying to get the numbers in individual form added to a list.
For example, if i input 3245, i want the list to say
["3","2","4","5"]
I have tried using a for in statement but got back numbers ranging from 0 - 3244 which was expected.
Any insight would be greatly appreciated, i am very new to python and am trying to teach myself code and re-write all my projects for school that was done in c to turn them into python. NOTE: i am using python 3, not 2.
Here is the rest of my code if it helps.
cc = []
card = int(input("Credit Card: "))
for n in range(card):
cc.append(n)
print(cc)
Upvotes: 0
Views: 42
Reputation: 11
you can just convert the integer to string and iterate through every character of the string and during the iteration just append to cc.
cc = []
card = int(input("Credit Card: "))
for i in str(card):
cc.append(i)
print(cc)
Upvotes: 0
Reputation: 1684
a = 3245
b = list(str(a))
print(b)
The above code can convert an integer to a list of characters. First convert the integer to a string and then convert the string to a list form.
Upvotes: 0
Reputation: 2521
First of all, you should either accept the input number as string or convert it to string. That way, you can just parse through each character in the string and add them to the list. Currently you are getting the number 0-3244 because of you are looping for the amount of inputted number and adding the loop index to your list. Therefore, this should do what you want
cc = []
card = input("Credit Card: ") # or str(int(input("Credit Card: ")))
# if you want to restrict it to integer
for n in range(len(card)): # loop by number of characters
cc.append(card[n]) # append each character
print(cc)
Upvotes: 2