Hello World 2, in Python 3
Hello World is the famous minimal introductory program for many programming languages. But as soon as it’s served its purpose, it’s discarded in the dust of history for more exciting things, such as data types or expressions… This is politically incorrect discrimination and must be stopped! This loyal and true didactic program should have the right to features and bugs just like any other! So ladies and gentlemen, please welcome the New and Improved Hello World, version 2.0!
Just to remind you, the traditional first program in Python (and other programming languages) is “Hello world!”:
Type this after the ‘>>>’ prompt in your interpreter, or into your IDE. Run it and be greeted in the time-honoured tradition of most introductory programming texts! But, you’re itching to get on with some serious programming, so let’s make this more interesting!
What’s My Name?
Don’t greet ‘World’, greet me, by name! So, we need a way for the program to get my name. We could supply our name after typing the program name, like:
$ ./hello.py Monty
#!/usr/bin/python3 import sys if len(sys.argv) > 1: name = sys.argv print("Hello", name)
We need to import the sys module, to get access to the command-line arguments via sys.argv Note that we need to check if some were given; if not, the length of the sys.argv list would be 1 (the program name or path itself is in sys.argv), and trying to access the second element would upset Python. In which case we could just ask:
#!/usr/bin/python3 import sys if len(sys.argv) > 1: name = sys.argv else: name = input("Please tell me your name: ") if name == '': name = 'Monty print ("Hello", name, " You're wonderful!")
I have arranged for it to provide the default name of ‘Monty’ in the case that I still don’t give my name.
Is it Still Morning?
Now, let’s make it greet us appropriately to the time of day:
import datetime hour = datetime.datetime.now().hour name=input("Please tell me your name: ") if hour < 12: print ("Good morning", name) elif hour < 18: print ("Good afternoon", name) else: print ("Good evening", name) print ("You're wonderful!")
Not quite a Chatbot…
Nice, but now recall that the usual human convention on greeting someone, is to follow it up with a little small talk. Like this:
#!/usr/bin/python3 import sys import random import datetime remarks=['Isn\'t it a nice day!', 'How are you today?', 'I hear there was another shooting'] reply=['Really?', 'Oh wow!', 'Yeah...','Cool!'] if len(sys.argv) > 1: name = sys.argv else: name = input("Please tell me your name: ") if name == '': name = 'Monty' hour = datetime.datetime.now().hour if hour < 12: print ("Good morning", name) elif hour < 18: print ("Good afternoon", name) else: print ("Good evening", name) input (random.choice(remarks)) print (random.choice(reply), "Oh well, see you later!\n")
What the Internet was Built for
Would you like a cat picture with that? Of course you would! Just append the following lines to the program, and feel free to move the import statements to the top:
import urllib from PIL import Image catapi="http://thecatapi.com/api/images/get?format=xml&results_per_page=1" response = urllib.request.urlopen(catapi, timeout = 5) imgurl = response.read().split()[5:-6].decode("utf-8") Image.open(urllib.request.urlopen(imgurl, timeout = 5)).show()
Note that this is not an example of a well-written program – I’ll improve it in my next post.
Command Line too Hard for You?
In case you’d prefer a GUI for your greeting program, here is a minimal example. I leave the inclusion of the new features (& bugs?) as an ‘exercise for the reader’.
from tkinter import * root = Tk() w = Label(root, text="Hello, Monty!") w.pack() root.mainloop()
What other features do you think the new Hello World program should have? Did you spot the bug? Leave your suggestions below!