Your First Program

Let's wrap this up by combining what we learned into a nice little program:

>>> def say_hi(name):
...     if name == '':
...         print("You didn't enter your name!")
...     else:
...         print("Hi there...")
...         for letter in name:
...             print(letter)
... 
>>> name = input()
 < enter your name at this point >
>>> say_hi(name)

I managed to cram in one more new thing, the built-in function input(). It does exactly what you expect it to do: ask for input and assign that input to a variable.

When typing in this code, you have to be very careful with indentation. At this point, using the interactive Python shell starts to work against us. Luckily, we can also store our Python programs in files.


If you liked this page, please share it with a fellow learner: