lc-python-intro icon indicating copy to clipboard operation
lc-python-intro copied to clipboard

The last solution in "18-style.md" throw an error as the variable "s" is not defined.

Open PNZ-save opened this issue 3 years ago • 1 comments

Please delete the text below before submitting your contribution.


def string_machine(input_string, iterations): """ Takes input_string and generates a new string with -'s and *'s corresponding to characters that have identical adjacent characters or not, respectively. Iterates through this procedure with the resultant strings for the supplied number of iterations. """

"Assigned the input_string to variable s so that it can be used later in the for loop, otherwise the code throw an error"
s = input_string

print(input_string)
old = input_string
for i in range(iterations):
    new = ''
    # iterate through characters in previous string
    for j in range(len(s)):
        left = j-1
        right = (j+1)%len(s) # ensure right index wraps around
        if old[left]==old[right]:
            new += '-'
        else:
            new += '*'
    print(new)
    # store new string as old
    old = new

string_machine('et cetera', 10)


PNZ-save avatar Jun 11 '22 09:06 PNZ-save

Many thank for reporting this, @PNZ-save!

konrad avatar Jul 03 '22 09:07 konrad

Closing since a major update to the lesson will soon be merged, deprecating most standing issues. Thanks for your contribution!

chennesy avatar Jun 14 '24 13:06 chennesy