0

I'm trying to prompt the user with the help of list comprehension prompting the user with a raw_input in a specific range (3). I assign then the result to a variable called prompt_user like so:

prompt_user = j = [input('Type Here Value') for i in range(3)]
print(prompt_user)

I then call back my list comprehension to prompt the user:

j

However, it does not prompt the user back and it keeps the previous values, regardless of the fact that I've placed a raw_input in it? Now I've already checked the previous stack article however could not find a solution, as I am aware that list comprehension will return a list, however with the raw_input, I was expecting python to stop the run and prompt the user back no matter what. Instead, it uses the prompt input just the first time and then discards it completely. What am I doing wrong? Thanks

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
ChameleonX
  • 87
  • 1
  • 10
  • 2
    You are just assigning the result of the list comprehension to `j`. If you want to call `j()` you need to make a function out of it. – Darkonaut Feb 11 '18 at 07:44
  • 1
    ... `input` is *evaluated* each iteration of the comprehension and added to the list. It evaluates to a `str`. The comprehension itself is evaluated to a `list`. Not sure why you are expecting it to work differently. It sounds like you want a function. In any event, `input` in a list-comprehension strikes me as bad style: mixing list-comprehensions with side-effects... – juanpa.arrivillaga Feb 11 '18 at 07:46
  • @Darkonaut kindly post it as the answer and I'll assign it to you. – ChameleonX Feb 11 '18 at 07:55
  • 1
    Not necessary for this one, but thanks! – Darkonaut Feb 11 '18 at 08:00

2 Answers2

1

As per my understanding, you want to prompt the value user is inputting and store it. The following solution might work:

users = []
for _ in range(3):
    user = input('Enter here: ')
    print('Your value:{}'.format(user))
    users.append(user)

output:

Enter here: one
Your value:one
Enter here: Two
Your value:Two
Enter here: THree
Your value:THree
user3415910
  • 440
  • 3
  • 5
  • 19
0

The credit to @Darkonaut, needed to assign it a function to keep promoting and call back the function not to a variable regardless of the raw_input.

ChameleonX
  • 87
  • 1
  • 10