Skip to content

getKeywords() ordinal ladder produces "21th" and the keyword count is a magic number #10

Description

@dmccoystephenson

getKeywords() builds its prompt text from a hardcoded four-branch ordinal ladder:

numKeywords = 10
for i in range(numKeywords):
    if i == 0:
        keyword = input("Enter 1st keyword: ")
        ...
    elif i == 1:
        keyword = input("Enter 2nd keyword: ")
        ...
    elif i == 2:
        keyword = input("Enter 3rd keyword: ")
        ...
    else:
        keyword = input("Enter " + str(i+1) + "th keyword: ")
        ...

Two problems are combined in this block.

The ordinal ladder is wrong past 20

The else branch appends "th" unconditionally, so the 21st through 23rd prompts read 21th, 22th, 23th. Evaluated directly:

$ python3 -c "print([('1st' if i==0 else '2nd' if i==1 else '3rd' if i==2 else str(i+1)+'th') for i in [0,1,2,3,20,21,22]])"
['1st', '2nd', '3rd', '4th', '21th', '22th', '23th']

This is latent rather than user-visible today, since numKeywords never exceeds 10.

The keyword count is a magic number

numKeywords = 10 is a local variable with no way to change it short of editing the source. Raising it is also not safe on its own: an odd value would trigger the createPairs() IndexError filed separately.

Suggested resolution

The four-branch ladder should be replaced by a single ordinal-suffix expression that is correct for every value, and the count should be lifted somewhere it can be changed deliberately. Note that the prompt strings are the program's user interface, so the text for the first ten prompts should remain byte-identical.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions