teemulehtinen / qlcpy

Generates questions about constructs and patterns in a given program
MIT License
1 stars 0 forks source link

Variables first defined in if statements results in confusing questions #3

Open coderforlife opened 1 month ago

coderforlife commented 1 month ago

For example, the following code (which is lines 31-47) with an argument of 21:

def get_ordinal(number):
    """
    Get the ordinal number (1st, 2nd, 3rd, 4th, etc.) for a number (1, 2, 3, 4, etc.)
    Handles 11, 12, and 13 as special cases. Works for any integer up to 100.
    """
    last_digit = number % 10
    if number in (11, 12, 13):
        suffix = "th"  # line 38
    elif last_digit == 1:
        suffix = "st"  # line 40
    elif last_digit == 2:
        suffix = "nd"  # line 42
    elif last_digit == 3:
        suffix = "rd"
    else:
        suffix = "th"  # line 46
    return f"{number}{suffix}"  # line 47

Had the question generated:

A value is assigned to variable suffix on line 42. On which line is suffix created? A. 38 B. 43 C. 46 D. 47

The variable on line 42 is created on line 42, not line 38 (which was never run). In fact, line 42 itself never runs (line 40 does, and that is the line that created the variable, but it isn't a possible answer). But if we don't account for the actual argument given to the function, the answer would be 42, which is not a choice given.

teemulehtinen commented 1 month ago

I agree (will fix in few days). The suggested fix is to skip this question type (VariableDeclaration) for cases where a variable can be first created in different branches. This question type is supposed to be easy and not require actual code tracing. The question that asks for values assigned to a variable (VariableTrace) can be used to address this type of knowledge where required.