10. For your hobbies (small doubt)


#1

I've submitted my code and it's working fine. I was having some trouble in getting it but meanwhile i've clarified it. How ever i have a question related with the result.

So everytime i write the hobby it prints the hobby and the number correspondent to answer given. Like i show under:

Your hobby:  sport
[0]
Your hobby:

Is it possible to hide or write it in a way that the number is not visible?


#2

Hi @pedro.mac,

Do you have an extra print statement or more than one call to the raw_input function within the loop that is causing the number to display?

Is this the format of what you would like to have in the console?

Hobby 1:  Music
Hobby 2:  Hiking
Hobby 3:  Programming

#3

Ok, so this is my code:

hobbies = []

Add your code below!

for hobby in range(3):
raw_input("Your hobby: ")
hobbies.append(hobby)
print hobbies

and when i submit it and fill it with some hobbies and this is the result in the console:

Your hobby: sport
[0]
Your hobby: read
[0, 1]
Your hobby: movies
[0, 1, 2]
None

What i'm trying to understand is if it is possible to not print the numbers that are printed after each hobby?!


#4

Yes, it is definitely possible and better to do it that way.

The reason that the numbers are getting displayed is that your code is appending a counter, which is a number, to hobbies instead of appending the user's input to that list. When the user enters something you are not saving that input.

Here, the purpose of the variable is to serve as counter ...

for hobby in range(3):

You are using the variable hobby as a counter. Instead, give the variable a name that is more commonly used for a counter, such as i ...

for i in range(3):

In any case, that variable will refer to some counting numbers, and should not be the same variable as what you append to hobbies.

Now within the loop, you can get the user's input, save it in the variable hobby, and append it to hobbies ...

    hobby = raw_input("Your hobby: ")
    hobbies.append(hobby)

After the loop ...

print hobbies

The output might not look nice in that format, so you might rather do this ...

for hobby in hobbies:
    print hobby

Then each hobby gets neatly displayed on its own line.


#5

That was really very well explained i really understood very clearly.

That code is actually very neat.

I know that we're creating a loop and because of that, each time Python ends reading the code he's going to print the input, but (maybe now i'm just wondering something impossible) is it possible to write the code in a way that, when it's showed in the console it doesn't print the hobby each time you submit it or in the end and just saves it?

(Don't know if i'm being clear...)


#6

Are you asking whether there is a way that the input can be entered without it being visible on the screen? I don't know how to do that.


#7

To accomplish this, you just need to put the for loop

for hobby in hobbies:
print hobby

outside of the other for loop. This will print the hobbies after storing them.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.