Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Simply input syntax question

Watch Python coding section and the teacher kinda rushed through the basic input syntax.

What exactly does input("> ") syntax mean. I realize your adding to a list but does this mean your adding forever?

Example: new item =input("> ")

1 Answer

Very simply put, input creates a dialogue for you in the console and returns whatever you type into that dialogue.

First, and probably most simple, is to call it for a single variable.

name = input("What is your name? ") 
print(name)

You can also specify how many times for this to happen using a loop.

for a in range(5):
    favorite_food = input("Tell me your 5 favorite foods: ")
    print(favorite_food)

Alternatively you can also have it loop forever with a while statement.

while True:
    favorite_food = input("Tell me your 5 favorite foods: ")
    print(favorite_food)

Of course it's not suggested to do this unless you give it a way to escape the loop. Otherwise it will keep taking your inputs forever! This code will never actually reach print.

Later on you can make this more complex by storing the values in a list and displaying it at the end.

listx = []
for a in range(3): #loop 5 times
    listx.append(input("Tell me your 3 favorite video games: ")) #Add your answers to a list
print(listx) #Print out your list with 3 answers