User input - Python

Last edited: 24th June 2022

Hello World

'Hello World' is synonymous with being the first program you run to check you are in the right direction. It is also a good example of how to show information or ask questions on the screen for your program user to read.

Write the code below in your script and run it to check the terminal loads the program then writes "Hello World"

print("Hello World")

NOTE: Print means print/show on the screen, not send it to the printer for printing on paper :)

What is your name?

Close the terminal that ran your code (but not your IDE) so you can add to the 'Hello World' script:

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

When you run the code you will notice it pauses after "What is your name?". This is so you can write your name then press enter. The code is waiting for user input then storing it in a variable called 'name'. When you are more confident you can choose different names for your variables - as long as you can remember what each variable is for - hence giving it a name that tells us what information is stored in it.

Once the user as given an input() the program can use it in a print() function.

Try adding other questions and variables to collect answers then see if you can print() a sentence using them.


This lesson is part of the Simple Python Challenges Course

Agree
We use cookies to provide you with a better service. Continue browsing if you are happy with this.