Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65d2494

Browse files
authored
Merge pull requestalexmojaki#73 from alexmojaki/predict-output
Make users predict the output of certain steps
2 parents44be9ed +cbac84e commit65d2494

18 files changed

+948
-69
lines changed

‎backend/main/chapters/c03_variables.py‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
frommain.textimportMessageStep,Page,Step,VerbatimStep
88

99

10+
classword_must_be_hello(VerbatimStep):
11+
defcheck(self):
12+
ifself.console.locals.get("word")!="Hello":
13+
returndict(
14+
message="Oops, you need to set `word = 'Hello'` before we can continue."
15+
)
16+
returnsuper().check()
17+
18+
1019
classIntroducingVariables(Page):
1120

1221
classword_assign(VerbatimStep):
@@ -20,11 +29,11 @@ class word_assign(VerbatimStep):
2029

2130
program="word = 'Hello'"
2231

23-
classword_check(VerbatimStep):
32+
classword_check(word_must_be_hello):
2433
"""
2534
This creates a variable with the name `word` that refers to the string value `'Hello'`.
2635
27-
Check now that this is true by simply running `__program__` in the shell by itself.
36+
Now see what happens when you run `__program__` in the shell by itself.
2837
"""
2938

3039
program="word"
@@ -35,6 +44,8 @@ class word_string_check(VerbatimStep):
3544
"""
3645

3746
program="'word'"
47+
predicted_output_choices= ["word","'word'","Hello","'Hello'"]
48+
correct_output="'word'"
3849

3950
classsunshine_undefined_check(VerbatimStep):
4051
"""
@@ -44,21 +55,14 @@ class sunshine_undefined_check(VerbatimStep):
4455
"""
4556

4657
program="sunshine"
58+
predicted_output_choices= ["sunshine","'sunshine'","Hello","'Hello'"]
59+
correct_output="Error"
4760

4861
final_text="""
4962
The answer is that `sunshine` looks like a variable, so Python tries to look up its value, but since we never defined a variable with that name we get an error.
5063
"""
5164

5265

53-
classword_must_be_hello(VerbatimStep):
54-
defcheck(self):
55-
ifself.console.locals.get("word")!="Hello":
56-
returndict(
57-
message="Oops, you need to set `word = 'Hello'` before we can continue."
58-
)
59-
returnsuper().check()
60-
61-
6266
classUsingVariables(Page):
6367
title="Using Variables and `print()`"
6468

@@ -77,6 +81,9 @@ class name_assign(Step):
7781

7882
classassigned_something_else(MessageStep):
7983
"""Put `your_name` before the `=` to create a variable called `your_name`."""
84+
85+
# TODO this doesn't work if the user enters invalid syntax, e.g.
86+
# your name = 3, because check quits early due to a SyntaxError
8087
program="foo = 3"
8188

8289
defcheck(self):
@@ -122,6 +129,8 @@ class hello_plus_name(VerbatimStep):
122129
__program_indented__
123130
"""
124131

132+
# TODO add predicted output: requires computing choices in worker
133+
125134
program="'Hello ' + your_name"
126135

127136
classword_plus_name(word_must_be_hello):
@@ -220,6 +229,15 @@ class sentence_equals_word_plus_name(VerbatimStep):
220229
221230
__program_indented__
222231
"""
232+
233+
predicted_output_choices= [
234+
"sentence",
235+
"word + ' ' + name",
236+
"Hello + ' ' + World",
237+
"'Hello' + ' ' + 'World'",
238+
"Hello World",
239+
"'Hello World'",
240+
]
223241

224242
defprogram(self):
225243
word='Hello'
@@ -236,6 +254,15 @@ class sentence_doesnt_change(VerbatimStep):
236254
"""
237255
program_in_text=False
238256

257+
predicted_output_choices= [
258+
"Hello World\n"
259+
"Hello World",
260+
"Hello World\n"
261+
"Goodbye World",
262+
"Goodbye World\n"
263+
"Goodbye World",
264+
]
265+
239266
# noinspection PyUnusedLocal
240267
defprogram(self):
241268
word='Hello'

‎backend/main/chapters/c04_for_loops.py‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,18 @@ class hello_plus_equals(VerbatimStep):
232232
233233
__program_indented__
234234
235-
What do you think the line `hello = hello + '!'` does?What do you thinkthe program will output? Make a prediction, then run it to find out.
235+
What do you think the line `hello = hello + '!'` does?Runthe program to find out.
236236
"""
237237

238+
predicted_output_choices= [
239+
"Hello\n"
240+
"Hello",
241+
"Hello\n"
242+
"Hello!",
243+
"Hello!\n"
244+
"Hello!",
245+
]
246+
238247
defprogram(self):
239248
hello='Hello'
240249
print(hello)
@@ -253,13 +262,37 @@ class name_triangle(VerbatimStep):
253262
temp = hello
254263
hello = temp + '!'
255264
256-
This is very useful in a loop.Think about whatthis program will do, then run it:
265+
This is very useful in a loop.Try outthis program:
257266
258267
__program_indented__
259268
260269
By the way, `''` is called the *empty string* - a string containing no characters.
261270
"""
262271

272+
predicted_output_choices= [
273+
"W\n"
274+
"o\n"
275+
"r\n"
276+
"l\n"
277+
"d",
278+
"World",
279+
"W\n"
280+
"Wo\n"
281+
"Wor\n"
282+
"Worl\n"
283+
"World\n",
284+
"World\n"
285+
"Worl\n"
286+
"Wor\n"
287+
"Wo\n"
288+
"W\n",
289+
"World\n"
290+
"World\n"
291+
"World\n"
292+
"World\n"
293+
"World\n",
294+
]
295+
263296
defprogram(self):
264297
name='World'
265298
line=''
@@ -269,11 +302,28 @@ def program(self):
269302

270303
classname_triangle_missing_last_line(VerbatimStep):
271304
"""
272-
The details in the above program are important. Whatgoes wrong if you swap the last two lines and run this program instead?
305+
The details in the above program are important. Whathappens if you swap the last two lines and run this program instead?
273306
274307
__program_indented__
275308
"""
276309

310+
predicted_output_choices= [
311+
"W\n"
312+
"Wo\n"
313+
"Wor\n"
314+
"Worl\n"
315+
"World\n",
316+
"Wo\n"
317+
"Wor\n"
318+
"Worl\n"
319+
"World\n",
320+
"\n"
321+
"W\n"
322+
"Wo\n"
323+
"Wor\n"
324+
"Worl\n"
325+
]
326+
277327
defprogram(self):
278328
name='World'
279329
line=''

‎backend/main/chapters/c05_if_statements.py‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class excited_example(VerbatimStep):
6161
__program_indented__
6262
"""
6363

64+
predicted_output_choices= [
65+
'Hello World',
66+
'Hello World!',
67+
]
68+
6469
defprogram(self):
6570
sentence='Hello World'
6671
excited=True
@@ -76,6 +81,11 @@ class excited_false_example(VerbatimStep):
7681
"""
7782
program_in_text=False
7883

84+
predicted_output_choices= [
85+
'Hello World',
86+
'Hello World!',
87+
]
88+
7989
defprogram(self):
8090
sentence='Hello World'
8191
excited=False
@@ -133,6 +143,17 @@ class for_inside_if(VerbatimStep):
133143
134144
__program_indented__
135145
"""
146+
predicted_output_choices= [
147+
'Hello World',
148+
'!!!!!!!!!!!',
149+
'Hello World!!!!!!!!!!!',
150+
'!!!!!!!!!!!Hello World',
151+
'Hello World!',
152+
'!Hello World',
153+
'!Hello World!',
154+
'H!e!l!l!o! !W!o!r!l!d!',
155+
'!H!e!l!l!o! !W!o!r!l!d',
156+
]
136157

137158
defprogram(self):
138159
sentence='Hello World'
@@ -194,12 +215,18 @@ class print_tail(print_tail_base):
194215
__copyable__
195216
__program_indented__
196217
"""
218+
predicted_output_choices= [
219+
'Hello World',
220+
'ello World',
221+
'Hello Worl',
222+
'H',
223+
'd',
224+
]
197225

198226
classprint_tail_snoop(print_tail_base):
199227
"""
200-
As you can see, it prints everything but the first character. Take some time to understand how this works.
201-
202-
In fact, it's time to introduce a new tool to help you understand programs. Click the 'Snoop' button to run the same program while also showing what's happening.
228+
Programs are starting to get complicated!
229+
It's time to introduce a new tool to help you understand programs. Click the 'Snoop' button to run the same program while also showing what's happening.
203230
"""
204231

205232
program_in_text=False
@@ -369,6 +396,12 @@ class undefined_char(VerbatimStep):
369396
"""
370397
program_in_text=False
371398

399+
predicted_output_choices= [
400+
'Hello World',
401+
'Hello World!',
402+
]
403+
correct_output="Error"
404+
372405
# noinspection PyUnboundLocalVariable
373406
defprogram(self):
374407
sentence='Hello World'
@@ -835,6 +868,8 @@ class grades_example(VerbatimStep):
835868
Recall that `elif percentage < 60` after `if percentage < 40` means "if the percentage wasn't less than 40 and also is less than 60", so it will pass for all numbers from 40 to 59 inclusive. Similarly a 'B' is for percentages from 60 to 79, and an 'A' is for any number 80 and up.
836869
"""
837870

871+
predicted_output_choices= ["F","C","B","A"]
872+
838873
defprogram(self):
839874
percentage=73
840875

‎backend/main/chapters/c06_lists.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,55 @@ class range_len(VerbatimStep):
423423
__program_indented__
424424
"""
425425

426+
predicted_output_choices= ["""\
427+
This
428+
is
429+
a
430+
list
431+
""","""\
432+
0
433+
1
434+
2
435+
3
436+
""","""\
437+
0
438+
This
439+
1
440+
is
441+
2
442+
a
443+
3
444+
list
445+
""","""\
446+
This
447+
0
448+
is
449+
1
450+
a
451+
2
452+
list
453+
3
454+
""","""\
455+
0
456+
1
457+
2
458+
3
459+
This
460+
is
461+
a
462+
list
463+
""","""\
464+
This
465+
is
466+
a
467+
list
468+
0
469+
1
470+
2
471+
3
472+
""",
473+
]
474+
426475
defprogram(self):
427476
words= ['This','is','a','list']
428477

‎backend/main/chapters/c07_nested_loops.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class first_nested_loop(VerbatimStep):
1313
__program_indented__
1414
"""
1515

16+
# TODO after adding quotes chapter:
17+
# collapse first three steps into one, add predicted output
18+
1619
defprogram(self):
1720
forletterin"ABC":
1821
print(letter)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp