Now that we’ve covered the basics of variables, let us write a program that makes use of them. We’ll revisit the “Hello World” program we wrote in Chapter 2, but this time we’ll make it interactive. Instead of just saying hello to the world, we want the world to know our names and ages too. In order to do that, our program needs to be able to prompt us for information and display them on the screen.
Two built-in functions can do that for us: input() and print().
For now, let’s type the following program in IDLE. Save it and run it.
myName = input("Please enter your name: ")
myAge = input("What about your age: ")
print ("Hello World, my name is", myName, "and I am", myAge, "years old.")
The program should prompt you for your name.
Please enter your name:
Supposed you entered James. Now press Enter and it’ll prompt you for your age.
What about your age:
Say you keyed in 20. Now press Enter again. You should get the following statement:
Hello World, my name is James and I am 20 years old.