For these tasks, you might need to use the time.sleep function.
To do this, write the following at the top of the program:
import time
You can then use the following to make a delay in the program:
time.sleep(2)
Print Statements
Download the print.py file and save it to your own part of the server.
Once you have it opened you should see the following code:
Print(Hello world)
- Run this code and see what happens.
- Correct any errors in the code.
-
Using the
time.sleepfunction, write a program that asks the user to answer a joke and gives them 3 seconds before giving them the answer. For example:
Why should you never tell a joke to an egg?
[Wait three seconds]
Because it might crack up!
Input
We can use the input function to get user input:
print("Hello " + input("What's your name? ") + "!")
Change your program to take in information from the user.
- Write a program so that it asks the user for their name and then outputs it to the screen afterwards.
- Write a program to ask the user for the date of birth and display it to the screen
- Write a program to ask the user for both their first name and their surname and then print both on to the screen at the same time with one print statement.
-
Write a program to ask for the userโs name and the current time of day (morning/afternoon/evening). Then print a greeting like:
Good morning, Jamie! or Good evening, Jamie! depending on what they entered. -
Write a program to ask: "What is your favourite food?" and then ask "What is your favourite hobby?"
Then output: "So your favourite food is ___ and your favourite hobby is ___" in one line. -
Write a program that takes in three numbers and adds them together. Remember to convert them to an
int. -
Build a simple conversation:
Hi there! I'm ChatPy, your friendly robot. What's your name? >> John Hi John, it's nice to meet you. What day is it? >> Monday I hope you're having a great Monday!
In the next few lessons we'll be looking at different things such as variables and selection. Look at the code below.
if input("Do you like Python") == "yes": print("Yay! Me too!") else: print("Oh no! Youโll love it soon!")
- Adjust the code