Python: Need help making a simple program using arrays and loops
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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"
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.