Mariusz Andrzejewski
Mariusz Andrzejewski

Reputation: 39

Appending with lists in python using a counter to add specific items

I am new to programming with some experience in Visual Basic.

I am writing a python program where the user enters in a 4 digit number as an integer into a variable (or list, doesn't matter) and gets output AS a list in 4 digits.

IE. 4444 input, 4, 4, 4, 4 output.

I've written this mini function but I've misinterpreted the logic. The counter makes the process repeat four times; not go through each individual number. Missing a counter += 1 somewhere as well I guess to make it step through the four digit number.

Can anyone else help me here? I'm new to coding with loops.

output = list()
cache = list()
cache = input("Please enter the  number: ")
for counter in range(0,4):
    output.append(cache)
print(output)

Upvotes: 0

Views: 411

Answers (6)

user3698971
user3698971

Reputation: 307

I like the Jason's approach. I would like to propose one more. you can simplify your code using the following approach:

cache = input("Please enter the  number: ")
print [int(val) for val in str(cache)]

But depends what you would like to achieve.

Let me know if that works for you.

Upvotes: 0

RatDon
RatDon

Reputation: 3543

Since raw_input( ) in python takes each input as a string, this code will work fine for you.

x=raw_input("Enter a number: ")
for digit in list(x):
    print digit+",",

Input: 4567
Output: 4, 5, 6, 7,

Since you are using Python3, You can change the above code to this.

x=input("Enter a number: ")
for digit in list(x):
    print(digit, end=", ")

The output is same.

Note: This won't take exact 4 digits.

Upvotes: 0

lullis
lullis

Reputation: 304

Looks like you want a list comprehension:

print [int(c) for c in raw_input("Enter a 4 digit number:")[:4]]

Breaking it down:

  • The raw_input function will return you a string. The built-in input will do something else from what you want.
  • The [:4] will get only a slice of the string.
  • The list comprehension will get one character of the resulting string, convert it to an integer, and create another list for you.

Upvotes: 0

Jason B
Jason B

Reputation: 7465

To fix exactly what you had, the following works:

output = list()
cache = list()
cache = input("Please enter the  number: ")
for val in str(cache): # note you do not need to cast to a str in python 3.x
    output.append(int(val))
print(output)

You can simply convert the integer input into a string, and iterate over each character in the string. Python is pretty cool in that it pretty much lets you iterate over anything with the traditional for loop structure. You then append each individual part of the four digit integer - as you iterate through it - to the list (converting it back to an integer if desired).

What your original code does is append the entire integer (as defined by cache) to the new list output four different times. You were not looking at each part of the integer, but rather the integer in its entirety.

Note that you do not need to predefine cache before the assignment from input, and you can define a list using [] instead of calling the list() function. Furthermore, you should use raw_input instead of input, and then you do not need to cast the read in value to a string before processing. It is as follows:

output = []
cache = raw_input("Please enter the  number: ")
for val in cache:
    output.append(int(val))
print(output)

Finally, if you do not care about preserving the values of the number that you read in as integers (i.e. having them as strings is ok for you), you can simple call the list() function on the cache:

>> Enter a 4 digit number:1234
>> list(cache)
>> ['1', '2', '3', '4']

Let me know if this does the trick for you.

Upvotes: 1

smci
smci

Reputation: 33940

In case you don't know, you can just directly access the individual digits from an integer by:

from decimal import Decimal

Decimal(4321).as_tuple().digits
# (4, 3, 2, 1)

and you can directly get a 4-digit input from the input() function. Just convert it to int(), and catch any exception due to non-digit chars.

Upvotes: 0

Tom Barron
Tom Barron

Reputation: 1594

Your code will result in a list containing four copies of whatever the user inputs. If you want to limit the user's input to four digits, I would suggest something like the following:

cache = ""
while len(cache) < 4:
    cache = raw_input("Please enter four digits (any extras will be discarded): ")
if 4 < len(cache):
    cache = cache[0:4]
print list(cache)

If the user enters fewer than four digits, the while loop will reprompt until at least four digits are entered. If more than four digits are entered, the if statement cuts off everything after the fourth. Of course, this code does nothing to verify that the user entered digits -- it will accept "abcd" for example.

To turn a string into a list, just run it through list(). There's no need to pull it apart and append each character separately to the list.

Finally, I used raw_input() rather than input() because that's usually what works best in the real world.

Upvotes: 0

Related Questions