MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / has_printout

Function has_printout

pythonwhat/checks/has_funcs.py:623–745  ·  view source on GitHub ↗

Check if the right printouts happened. ``has_printout()`` will look for the printout in the solution code that you specified with ``index`` (0 in this case), rerun the ``print()`` call in the solution process, capture its output, and verify whether the output is present in the output of the

(
    state, index, not_printed_msg=None, pre_code=None, name=None, copy=False
)

Source from the content-addressed store, hash-verified

621
622
623def has_printout(
624 state, index, not_printed_msg=None, pre_code=None, name=None, copy=False
625):
626 """Check if the right printouts happened.
627
628 ``has_printout()`` will look for the printout in the solution code that you specified with ``index`` (0 in this case), rerun the ``print()`` call in
629 the solution process, capture its output, and verify whether the output is present in the output of the student.
630
631 This is more robust as ``Ex().check_function('print')`` initiated chains as students can use as many
632 printouts as they want, as long as they do the correct one somewhere.
633
634 Args:
635 index (int): index of the ``print()`` call in the solution whose output you want to search for in the student output.
636 not_printed_msg (str): if specified, this overrides the default message that is generated when the output
637 is not found in the student output.
638 pre_code (str): Python code as a string that is executed before running the targeted student call.
639 This is the ideal place to set a random seed, for example.
640 copy (bool): whether to try to deep copy objects in the environment, such as lists, that could
641 accidentally be mutated. Disabled by default, which speeds up SCTs.
642 state (State): state as passed by the SCT chain. Don't specify this explicitly.
643
644 :Example:
645
646 Suppose you want somebody to print out 4: ::
647
648 print(1, 2, 3, 4)
649
650 The following SCT would check that: ::
651
652 Ex().has_printout(0)
653
654 All of the following SCTs would pass: ::
655
656 print(1, 2, 3, 4)
657 print('1 2 3 4')
658 print(1, 2, '3 4')
659 print("random"); print(1, 2, 3, 4)
660
661 :Example:
662
663 Watch out: ``has_printout()`` will effectively **rerun** the ``print()`` call in the solution process after the entire solution script was executed.
664 If your solution script updates the value of `x` after executing it, ``has_printout()`` will not work.
665
666 Suppose you have the following solution: ::
667
668 x = 4
669 print(x)
670 x = 6
671
672 The following SCT will not work: ::
673
674 Ex().has_printout(0)
675
676 Why? When the ``print(x)`` call is executed, the value of ``x`` will be 6, and pythonwhat will look for the output `'6`' in the output the student generated.
677 In cases like these, ``has_printout()`` cannot be used.
678
679 :Example:
680

Callers 3

test_functionFunction · 0.90
test_function_v2Function · 0.90

Calls 3

has_outputFunction · 0.85
assert_execution_rootMethod · 0.80
findMethod · 0.80

Tested by 3

test_functionFunction · 0.72
test_function_v2Function · 0.72