Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
New to pyhton, I'm trying to learn it on my own while taking.... Posted by Dynomike on 28 Oct 2011 at 7:42 PM
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.

#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)
    

Report
Re: New to pyhton, I'm trying to learn it on my own while taking.... Posted by dreemzkayp on 29 Oct 2011 at 12:20 AM
: 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
Report
Re: New to pyhton, I'm trying to learn it on my own while taking.... Posted by Dynomike on 29 Oct 2011 at 3:32 AM
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

#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)




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.