Some useful snippets - Answers
There are always lots of ways of doing the same thing. Go to http://docs.python.org/3.3/library/time.html and have a look at this module. It contains some useful things.
Q1. Type in this code and get it working:
import time
print("Hello")
time.sleep(3)
print("Goodbye")
This code displays 'Hello', waits 3 seconds and then displays 'Goodbye'.
Q2. Write a program that:
displays '2 + 2 =' and then a 3 second wait should happen and then
'Wait for it. Wait for it ....' should be displayed and then a further delay of 3 seconds should happen and then
'4' should be displayed.
import time
print("2 + 2 = ")
time.sleep(3)
print("Wait for it. Wait for it ....")
time.sleep(3)
print("4")
Q3. Ask a user how long they want to wait for, and then wait for that number of seconds before displaying 'Finished'.
import time
selection = int(input("How long do you want to wait for? >>> "))
time.sleep(selection)
print("Finished")
Note: You can convert the input to a float for greater precision.
Q4. Look up http://docs.python.org/3.3/library/calendar.html and refer to it for some of the following questions.
Q5. Type in this code and get it working:
cal = calendar.month(1791, 12)
print ("Here is the calendar for December 1791, when Charles Babbage was born.\n")
print (cal);
Charles Babbage is often said to be the father of computing. This prints out a nicely formatted calendar of the month he was born in.
Q6. E.g.
cal = calendar.month(1962,8)
print ("Here is the calendar for the month I was born in.\n")
print (cal);
Q7. for the year e.g.
cal = calendar.calendar(1962)
print ("Here is the calendar for the year I was born.\n")
print (cal);
Q8. In the weekday method, 0 represents Monday.
Q9. Use the weekday method to find the day (Monday, Tuesday etc) you were born on e.g.
cal = calendar.weekday(1962,8,21)
print ("Here is the day I was born.\n")
print (cal);
Q10. Explore some of the other methods in the calendar module. Try some of them out.