Now
we will put together everything you have learned so far to make a game. The
game is a quiz called "Are you Sherlock, or are you Watson?" The user
answers four questions, and the program determines whether the user is
Sherlock, or Watson.
This
is how the program works. There is a variable called character_score, which is
initially set to zero. If the user answers a question in the style of Sherlock
Holmes, points are added to the score. If the user answers in the style of Dr.
Watson, points are subtracted from the score. After the questions have been
answered, if the score is positive, the program says the user is Sherlock. If
the score is negative, the program says the user is Watson. If the score is
exactly zero, the program, in a surprise ending, says the user is Mrs. Hudson (the
landlady).
Read
that paragraph again. Look for things that sound like printing, strings,
numbers, variables, user input, and decisions.
Then, create a new Python file, type the following and run it. It is a long program, but don't panic; everything about it is something you have already done. Just focus on small sections, one at a time.
1.
print "Are you Sherlock, or are you Watson?"
2.
3.
character_score = 0
4.
5.
6.
print ""
7.
print "It's the middle of the night, and you hear something in the street."
8.
print "What do you do?"
9.
print "1. I rush outside to see what's going on."
10. print "2. I try to go back to bed."
11. print "3. I can tell from the scuffling sound that it's a tall man with a limp."
12. answer = raw_input("> ")
13.
14. if answer == "1":
15. character_score = character_score - 5
16.
17. if answer == "3":
18. character_score = character_score + 5
19.
20.
21. print ""
22. print "You're talking, and your friend's phone starts ringing. He doesn't"
23. print "answer it. What do you do?"
24. print "1. I stop and ask him if he's going to answer it."
25. print "2. I stop and frown at him."
26. print "3. I grab the phone myself and hang it up."
27. answer = raw_input("> ")
28.
29. if answer == "2":
30. character_score = character_score - 3
31.
32. if answer == "3":
33. character_score = character_score + 5
34.
35.
36. print ""
37. print "A bomb is about to go off! What do you do?"
38. print "1. I notice that the wires aren't even connected properly,"
39. print " but I keep that to myself while I think about who might have planted it."
40. print "2. I look for the bomber so I can catch him and make him disarm it."
41. print "3. Oh dearie me. This is just dreadful."
42. answer = raw_input("> ")
43.
44. if answer == "2":
45. character_score = character_score - 5
46.
47. if answer == "1":
48. character_score = character_score + 4
49.
50.
51. print ""
52. print "You're talking, and you notice that your tea is cold. What do you do?"
53. print "1. I stop and say: my tea has gone cold!"
54. print "2. I walk towards the kitchen to warm it up or pour another cup."
55. print "3. I fling it out the window and keep talking."
56. answer = raw_input("> ")
57.
58. if answer == "2":
59. character_score = character_score - 4
60.
61. if answer == "3":
62. character_score = character_score + 7
63.
64.
65. if character_score > 0:
66. print "You are Sherlock. You are driven by a fear of boredom, leading you"
67. print "to solve cases and to learn new things. You don't sleep so well."
68. print "Why, you might find yourself awake at 3 in the morning learning"
69. print "to play the violin, or even to program computers."
70.
71. if character_score < 0:
72. print "You are Watson. You are hard working, faithful to your friends,"
73. print "and helpful to a fault. He's probably making you take this test,"
74. print "Sherlock is. You are indeed a good friend."
75.
76. if character_score == 0:
77. print "You are Mrs Hudson! You're not entirely sure what's going on,"
78. print "but if Sherlock says take the quiz, you'll take the quiz."
Here
is an explanation of this code:
Line
1 prints an introduction. Line 3 creates a variable we can use to count the
score.
Lines
6 through 11 print the first question, and line 12 asks for input and puts it
in the answer variable. Lines 14 through 18 have if statements for two of the
answers. There is not an if statement for the third answer, because
the third answer would just cause the score to change by zero, and that will
happen without us doing anything.
Lines
21 through 33 repeat the pattern for the second question: print the text, get
the input, and if it is equal to certain values, change the score.
Lines
36 through 48 repeat the pattern for the third question, and lines 51 through
62 repeat it for the fourth question.
Finally,
lines 65 through 78 have the last set of if statements. If the
score is greater than zero, we print the Sherlock answer. If it is less than 0,
we print the Watson answer. If it is 0, we print the Mrs. Hudson answer.
And
there you have it! These are the basic building blocks to make small games,
calculators, and a host of other programs: printing, numbers, strings,
variables, user input, and decisions.
You should take a break now. Have a walk! Watch some YouTube videos.