3.2. User input

Gathering information from users is a fundamental basic for any program. It is important you can ask for information then get your program to use it and produce an output.

3.2.1. 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 shows Hello World on the screen:

print("Hello World")

Note

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

3.2.2. 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.

3.2.3. Fahrenheit to Celsius Converter

Can you write a short program that asks a user for a Fahrenheit temperature and converts it to Celsius for them?

The following code will help you:

celsius=5*(farhenheit-32)/9

3.2.4. Morning or Afternoon?

Write a program that checks the time and greets a user with either ‘Good Morning’ or ‘Good Afternoon’. The following code will help:

import datetime
now=datetime.datetime.now()
comparisontime=now.replace(hour=12, minute=0, second=0, microsecond=0)