LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2005, 02:36 PM   #1
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Rep: Reputation: 30
Python: Need help making a simple program using arrays and loops


Hi all,
One of the first programs I like to try and make when first learning a programming language is a more advanced "hello world" type program. This is the basic flow of the program:
Code:
How many people are in the world? 3 
Person 1: Baix
Person 2: Bob 
Person 3: Tux 

Hello, Baix!
Hello, Bob!
Hello, Tux!
A loop would need to handle the imputing of the names (while i != total: ?) and the names would need to be put into an array to be printed using another loop to say hello to each name in the array.

I can't seem to figure out how to go about this in python. It seems to handle arrays (lists?) a bit differently than I've seen before. I'm sure once I see how this would be accomplished I would be able to figure out where I've been going wrong.

Thanks for any help!

Last edited by Baix; 08-01-2005 at 03:53 PM.
 
Old 08-01-2005, 03:36 PM   #2
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I am very new to programming, so I know just a little.

there are two common ways to store lists of data, the tuple, and the list
tuples are immutable, they can't be changed
lists are not immutable, they can be changed

you can set data to a tuple like this
array = (fist_element,second_element,third_element)

you can set data to a list like this
array = [fist_element,second_element,third_element]

the data in the list can be changed
array[1] = different_element

but with a tuple you ca't make any changes to the array, it is immutable.
 
Old 08-01-2005, 03:44 PM   #3
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Original Poster
Rep: Reputation: 30
I understand that part but having the user assign values to the array is whats confusing me. Thanks for the reply
 
Old 08-01-2005, 03:46 PM   #4
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
when you say user input, like using the read command with bash? Do you want the script to prompt you to enter input(names)?
 
Old 08-01-2005, 03:56 PM   #5
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Original Poster
Rep: Reputation: 30
I've gone back and italized what is inputed by the user. Basically it asks how many names it should ask for then asks for those names and stores them in an array. Then it spits the names back out saying "Hello, name"

Hope I'm making sense :-D.
 
Old 08-01-2005, 03:56 PM   #6
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I think you are looking for the list method called append

Code:
>>> test_list =[]
>>> test_list.append('hh')
>>> print test_list
['hh']
edit//

this was added before reading your last post
 
Old 08-01-2005, 04:09 PM   #7
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
here is how I would do it
Code:
amount = int(raw_input('enter how many names you want to add to the list'))

name_list=[]  # this is needed to initialaize the list, so it can be appended to
for i in range(amount): # this will cause the loop to go three times
    name = raw_input('enter a name')
    name_list.append(name)

# this next loop loops thru it
for i in name_list:
    print "hello,",  i

Last edited by shanenin; 08-01-2005 at 04:11 PM.
 
Old 08-01-2005, 04:26 PM   #8
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Original Poster
Rep: Reputation: 30
Thanks a lot! Just had to clean it up a bit, that works just fine. I didn't know about the append nor how to initilize it.

Once again, thanks! Looks like I've got a bit more reading ahead of me
 
Old 08-01-2005, 04:28 PM   #9
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
your welcome. I am just glad I have something to offer.
 
Old 08-01-2005, 05:40 PM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Hey im not trying to hijack the thread but im only familliar with C

in reading this it has made me wonder about arrays

in this language can you specify and array without a size like a varible array for the program ?
 
Old 08-01-2005, 09:15 PM   #11
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
not quite sure what you are saying.
 
Old 08-13-2005, 11:26 PM   #12
lmmier00
LQ Newbie
 
Registered: Aug 2005
Posts: 1

Rep: Reputation: 0
For the "hello world" problem to be coded in python a for loop would be used. Code could be something like what follows:

population_number = input("How many people in the world? ")
person_list = []
for item in range(1, population_number + 1):
person = raw_input("Person " + str(item) + ": ")
person_list.append(person)
for x in person_list:
print "Hello " + x

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: full string isn't making it through socket cs-cam Programming 1 11-26-2005 11:20 PM
how do I do this in Python? (simple sh script) johnMG Programming 6 12-22-2004 10:05 PM
Making a Python program gamehack Programming 0 04-11-2004 05:12 AM
Class Arrays in Python rootyard Programming 1 02-13-2004 10:58 PM
exiting program loops epoo Linux - Newbie 1 12-14-2003 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration