Skip to content

Commit 6484f3d

Browse files
committed
Update funcWithStrsAndConds.ptx
1 parent a0c527d commit 6484f3d

1 file changed

Lines changed: 320 additions & 1 deletion

File tree

pretext/functions/funcWithStrsAndConds.ptx

Lines changed: 320 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ a string covers more than one line. </p>
243243
</exercise>
244244
<exercise label="fsc_ac_pogil_initials_v2">
245245
<statement>
246-
<p>Run the code below to see what it prints. Then fix it to pass the given test. It should return a string with the
246+
<p>Run the code below to see what it prints. Then fix it to pass the test shown below the code. It should return a string with the
247247
first character of the first name and first character of the last name.</p>
248248
</statement>
249249
<program xml:id="fsc_ac_pogil_initials_v2_editor" interactive="activecode" language="python">
@@ -279,6 +279,7 @@ myTests().main()
279279
<subsection xml:id="fsc_string-slices">
280280
<title>String Slices</title>
281281
<exercise label="fsc_fitb_pogil_short_name">
282+
<p>You can get a copy of part or all of a string using <c>str_name[start:end]</c>.</p>
282283
<statement>
283284
<p>What is the last thing that will be printed when the code below runs? <var/> </p>
284285
</statement>
@@ -340,6 +341,324 @@ main()
340341
</var>
341342
</setup>
342343
</exercise>
344+
345+
<exercise label="fsc_fitb_string_nchars">
346+
<statement>
347+
<p>What built-in function tells you the number of characters in a string? <var/> </p>
348+
</statement>
349+
<setup>
350+
<var>
351+
<condition string="len">
352+
<feedback>
353+
<p>The len function takes a string and returns the number of characters in it.</p>
354+
</feedback>
355+
</condition>
356+
<condition string=".*">
357+
<feedback>
358+
<p>Look at the example code above.</p>
359+
</feedback>
360+
</condition>
361+
</var>
362+
</setup>
363+
</exercise>
364+
343365
</subsection>
366+
367+
<subsection xml:id="fsc_basic-conditionals-and-tests">
368+
<title>Basic Conditionals and Tests</title>
369+
<exercise label="fsc_fitb_return_temp_first_line">
370+
<statement>
371+
<p>What is the first thing (first line of text) that will be printed when the code below runs? <var/> </p>
372+
</statement>
373+
<setup>
374+
<var>
375+
<condition string="Baby,\s+its\s+cold\s+outside!">
376+
<feedback>
377+
<p>Since temp &lt; 32 this will print.</p>
378+
</feedback>
379+
</condition>
380+
<condition string=".*">
381+
<feedback>
382+
<p>Which conditional is true when temp &lt; 32?</p>
383+
</feedback>
384+
</condition>
385+
</var>
386+
</setup>
387+
</exercise>
388+
<exercise label="fsc_ac_pogil_return_temp_desc">
389+
<statement>
390+
<p>Run this code to see what it prints.</p>
391+
</statement>
392+
<program xml:id="fsc_ac_pogil_return_temp_desc_editor" interactive="activecode" language="python">
393+
<input>
394+
# function definition
395+
def get_temp_desc(temp):
396+
if temp &lt; 32:
397+
return "Baby, its cold outside!"
398+
elif temp &lt; 70:
399+
return "Wear a coat"
400+
elif temp &lt; 80:
401+
return "Feels great!"
402+
else:
403+
return "Too hot to handle!"
404+
405+
# function definition
406+
def main():
407+
print(get_temp_desc(20))
408+
print(get_temp_desc(85))
409+
410+
# function call
411+
main()
412+
</input>
413+
</program>
414+
</exercise>
415+
<exercise label="fsc_fitb_pogil_exec_false">
416+
<statement>
417+
<p>What keyword specifies the block of statements to execute if a Boolean condition is false? <var/> </p>
418+
</statement>
419+
<setup>
420+
<var>
421+
<condition string="else">
422+
<feedback>
423+
<p>The keyword else is used to execute a blocks of statements if a Boolean condition is false</p>
424+
</feedback>
425+
</condition>
426+
<condition string="Else">
427+
<feedback>
428+
<p>Most keywords in Python start with a lowercase letter</p>
429+
</feedback>
430+
</condition>
431+
<condition string=".*">
432+
<feedback>
433+
<p>Try again!</p>
434+
</feedback>
435+
</condition>
436+
</var>
437+
</setup>
438+
</exercise>
439+
<exercise label="fsc_fitb_pogil_conditional_start">
440+
<statement>
441+
<p>What keyword starts a conditional statement and is the only required keyword in it? <var/> </p>
442+
</statement>
443+
<setup>
444+
<var>
445+
<condition string="if">
446+
<feedback>
447+
<p>The keyword if is used to start a conditional statement and is the only required keyword.</p>
448+
</feedback>
449+
</condition>
450+
<condition string="If">
451+
<feedback>
452+
<p>Most keywords in Python start with a lowercase letter</p>
453+
</feedback>
454+
</condition>
455+
<condition string=".*">
456+
<feedback>
457+
<p>Try again!</p>
458+
</feedback>
459+
</condition>
460+
</var>
461+
</setup>
462+
</exercise>
463+
<exercise label="fsc_fitb_pogil_multi_conditonal">
464+
<statement>
465+
<p>What keyword is used in a conditional statement when you want three of more possible outcomes? <var/> </p>
466+
</statement>
467+
<setup>
468+
<var>
469+
<condition string="elif">
470+
<feedback>
471+
<p>The keyword elif is used to provide more than two possible outcomes to a conditional statement.</p>
472+
</feedback>
473+
</condition>
474+
<condition string="Elif">
475+
<feedback>
476+
<p>Most keywords in Python start with a lowercase letter</p>
477+
</feedback>
478+
</condition>
479+
<condition string=".*">
480+
<feedback>
481+
<p>Try again!</p>
482+
</feedback>
483+
</condition>
484+
</var>
485+
</setup>
486+
</exercise>
487+
<exercise label="fsc_ac_pogil_return_temp_desc_v2">
488+
<statement>
489+
<p>Modify the code in the main method below to test all possible return values from get_temp_desc.</p>
490+
</statement>
491+
<program xml:id="fsc_ac_pogil_return_temp_desc_v2_editor" interactive="activecode" language="python">
492+
<input>
493+
# function definition
494+
def get_temp_desc(temp):
495+
if temp &lt; 32:
496+
return "Baby, its cold outside!"
497+
elif temp &lt; 70:
498+
return "Wear a coat"
499+
elif temp &lt; 80:
500+
return "Feels great!"
501+
else:
502+
return "Too hot to handle!"
503+
504+
# function definition
505+
def main():
506+
print(get_temp_desc(20))
507+
print(get_temp_desc(85))
508+
509+
# function call
510+
main()
511+
</input>
512+
</program>
513+
</exercise>
514+
<exercise label="fsc_pogil_check-guess-Parsons-v2" numbered="yes" adaptive="yes" indentation="hide" language="python">
515+
<statement>
516+
<p>Put the blocks in order to define the function <c>check_guess</c> which will return <c>'too low'</c> if the guess is less
517+
than the passed target, <c>'correct'</c> if they are equal, and <c>'too high'</c> if the guess
518+
is greater than the passed target. For example, <c>check_guess(5,7)</c> returns
519+
<c>'too low'</c>, <c>check_guess(7,7)</c> returns <c>'correct'</c>, and <c>check_guess(9,7)</c> returns
520+
<c>'too high'</c>. There are three extra blocks that are not needed in a correct solution.</p>
521+
</statement>
522+
<blocks>
523+
<block order="1">
524+
<cline>def check_guess(guess, target):</cline>
525+
</block>
526+
<block order="2">
527+
<choice correct="yes">
528+
<cline> if guess &lt; target:</cline>
529+
</choice>
530+
<choice>
531+
<cline> if guess &lt; target </cline>
532+
</choice>
533+
</block>
534+
<block order="9">
535+
<choice correct="yes">
536+
<cline> return 'too low'</cline>
537+
</choice>
538+
<choice>
539+
<cline> return "too low' </cline>
540+
</choice>
541+
</block>
542+
<block order="8">
543+
<choice correct="yes">
544+
<cline> elif guess == target:</cline>
545+
</choice>
546+
<choice>
547+
<cline> elif guess = target: </cline>
548+
</choice>
549+
</block>
550+
<block order="6">
551+
<cline> return 'correct'</cline>
552+
</block>
553+
<block order="4">
554+
<cline> else:</cline>
555+
</block>
556+
<block order="7">
557+
<cline> return 'too high'</cline>
558+
</block>
559+
</blocks>
560+
</exercise>
561+
<exercise label="fsc_pogil_get_last_half_v2">
562+
<statement>
563+
<p>Finish the function <c>last_half(str)</c> which
564+
returns the last half of the characters from the passed string <c>str</c>.
565+
If <c>str</c> has less than 2 characters then return the empty string <c>""</c>.
566+
For example, <c>last_half("a")</c> returns <c>""</c>, <c>last_half("coal")</c> returns <c>"al"</c>, and <c>last_half("bye")</c> returns <c>ye</c>.</p>
567+
</statement>
568+
<program xml:id="funct_pogil_get_last_half_editor" interactive="activecode" language="python">
569+
<input>
570+
def last_half(str):
571+
572+
====
573+
from unittest.gui import TestCaseGui
574+
575+
class myTests(TestCaseGui):
576+
577+
def testOne(self):
578+
self.assertEqual(last_half('a'), '', "last_half('a')")
579+
self.assertEqual(last_half('coal'), 'al', "last_half('coal')")
580+
self.assertEqual(last_half('bye'), 'ye', "last_half('bye')")
581+
self.assertEqual(last_half('abcd'), 'cd', "last_half('abcd')")
582+
self.assertEqual(last_half('12345'), '345', "last_half('12345')")
583+
self.assertEqual(last_half('123456'), '456', "last_half('123456')")
584+
self.assertEqual(last_half('ab'), 'b', "last_half('ab')")
585+
self.assertEqual(last_half(''), '', "last_half('')")
586+
587+
588+
myTests().main()
589+
</input>
590+
</program>
591+
</exercise>
592+
<exercise label="fsc_fitb_cond_predict_grade">
593+
<statement>
594+
<p>What is the first thing that will be printed when the code below runs? <var/> </p>
595+
</statement>
596+
<setup>
597+
<var>
598+
<condition string="D">
599+
<feedback>
600+
<p>Since every if will execute it will print D.</p>
601+
</feedback>
602+
</condition>
603+
<condition string=".*">
604+
<feedback>
605+
<p>Remember that every if will execute. What is the final value of grade?</p>
606+
</feedback>
607+
</condition>
608+
</var>
609+
</setup>
610+
</exercise>
611+
<exercise label="fsc_ac_fix_grade_code">
612+
<statement>
613+
<p>Run this code to see what it prints. The modify it to work correctly. Next,
614+
add code to the <c>main</c> function to test each possible letter grade. It
615+
should return <c>A</c> if the score is
616+
greater than or equal 90, <c>B</c> if greater than or equal 80, <c>C</c> if greater
617+
than or equal 70, <c>D</c> if greater than or equal 60, and otherwise <c>E</c>.</p>
618+
</statement>
619+
<program xml:id="fsc_ac_fix_grade_code_editor" interactive="activecode" language="python">
620+
<input>
621+
# function definition
622+
def get_grade(score):
623+
grade = None
624+
if score &gt; 90:
625+
grade = 'A'
626+
if score &gt; 80:
627+
grade = 'B'
628+
if score &gt; 70:
629+
grade = 'C'
630+
if score &gt; 60:
631+
grade = 'D'
632+
else:
633+
grade = 'E'
634+
return grade
635+
636+
def main():
637+
print(get_grade(95))
638+
639+
main()
640+
</input>
641+
</program>
642+
</exercise>
643+
<exercise label="fsc_fitb_cond_grade_num_tests">
644+
<statement>
645+
<p>How many test cases do you need to check that the code above works as intended? <var/> </p>
646+
</statement>
647+
<setup>
648+
<var>
649+
<condition number="9">
650+
<feedback>
651+
<p>You need to test greater than and equal for each grade from A to D and also test a value less than 60.</p>
652+
</feedback>
653+
</condition>
654+
<condition string=".*">
655+
<feedback>
656+
<p>Remember that you should check that it works correctly if the value is greater than or equal the specified value.</p>
657+
</feedback>
658+
</condition>
659+
</var>
660+
</setup>
661+
</exercise>
662+
</subsection>
344663
</section>
345664
</worksheet>

0 commit comments

Comments
 (0)