Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

New to pyhton, I'm trying to learn it on my own while taking....

a programming logic class using pseudocode as primary language.

****

I'm trying to prompt the user for 5 items,
my goal is to set these items into a list,
then print the list.
I'm stuck at this point, all it does is print last inputed item.

[code]#List[items] and print them.

promptcounter = 0

while promptcounter < 6:
print('input up to 5 items.' + str(promptcounter))
list = []
item = input()
item = str(item)
list.append(item)
promptcounter = promptcounter + 1
print (list)

[/code]

Comments

  • : item = input()
    : item = str(item)
    : list.append(item)
    : promptcounter = promptcounter + 1
    : print (list)
    :
    : [/code]:
    :

    If you put the line
    print(list)
    immediately following the append line, you'll see that list isn't actually being appended with item at all. It's merely being made equal to the value of item. This explains why it seems to only print the last item to be input.
    Hope that helps steer you in the right direction.

    Pat
  • thanks! i totally understand now, it kept redefining list[] as a blank list, appending only the last inputed item during the last itteration of the while loop

    [code]#List[items] and print them.

    promptcounter = 1

    list = []

    while promptcounter < 6:
    print('input up to 5 items.' + str(promptcounter))
    promptcounter = promptcounter + 1
    item = input()
    item = str(item)
    list.append(item)

    print (list)[/code]
Sign In or Register to comment.