lc-python-intro
lc-python-intro copied to clipboard
The last solution in "18-style.md" throw an error as the variable "s" is not defined.
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)
Many thank for reporting this, @PNZ-save!
Closing since a major update to the lesson will soon be merged, deprecating most standing issues. Thanks for your contribution!