0

Im building a program in which you can enter the tasks you have to do per day. Now I'm trying to make the program print a list of all the tasks when you're done adding them. This is the code I have:

for x in range(0,len(tuesday)):
     print(" -",x)

tuesday is an array that contains all the tasks for that day. However, this code doesn't work; it just prints some numbers. How can I make the for loop print all the tasks from the tuesday array?

1
  • How do you initialize the array? Commented Jan 11, 2014 at 20:29

2 Answers 2

2

You can do it like this:

tuesday = ["feed the dog", "do the homework", "go to sleep"]

for x in tuesday:
    print x

would print:

feed the dog
do the homework
go to sleep
Sign up to request clarification or add additional context in comments.

3 Comments

Or if you specifically need to use the index for whatever reason, for x in range(0, len(tuesday)): print tuesday[x]
@tripleee: if we need the index, we should probably write for i,x in enumerate(tuesday):.
if you "need the index" ther is a better approach = enumerate()
0
for x in range(0,len(tuesday)):
     print(" -",x)

This code is not printing "random" numbers per se. It is printing the indices of all the elements in the tuesday array.

If you want to print the elements themselves, you will have to use this code:

for x in range(0,len(tuesday)):
 print(" -",tuesday[x])

or

for x in tuesday:
 print (x)

2 Comments

Thx! This is useful. Is probably better to use the second option but it won't harm me knowing how to do it another way.
Yet another option would be "for i, value in enumerate(tuesday):".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.