0

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)
1
  • Which version of Python are you using? Commented Mar 20, 2015 at 15:59

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

input returns a string - str(cache) is redundant.
input returns an integer type if an integer is the input. Only raw_input makes it always a string.
Depends on the Python version - I suspect it is 3.x.
Ah yes, I was assuming Python 2.7. Updated my answer, thanks!
0

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.

1 Comment

Your last statement assumes Python v2.x.
0

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.

Comments

0

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.

4 Comments

OP doesn't necessarily want a list comprehension, just a valid solution.
Apologies, but "looks like you want X" is a common phrasing to indicate recommendation, not to be taken literally. About the solutions: we could go on providing an answer that shows how to make that inside a loop, or what was wrong by using range, but it seems that giving a pointer to list comprehensions and showing a solution that is the common idiom in Python is an adequate answer to a beginner Python programmer.
How about map(int, input("Enter a 4 digit number:")[:4])
Surely it works, but there are a few reasons to go with comprehensions. For example, map returns an iterator in Python 3 instead of a list. Moreover, for a beginner the concept of a list comprehension seems easier to grasp than map.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.