19.8k views
1 vote
The y and z keys swapping position is messing with your touch typing. You decide to write out your email as if the keys were in the correct position and then use Python to swap all ys and zs. Your task here is to write a function called fix_yz. This function takes a single argument which will be a string. Your function needs to return this string with all of the ys and zs swapped, and all of the Ys and Zs swapped. Here are some example calls to your function:

s = fix_yz('What did zou saz?')print(s)What did you say?s = fix_yz('Zour tip about the yoo was a great one!')print(s)Your tip about the zoo was a great one!s = fix_yz('We onlz have one week left')print(s)We only have one week left :(HintThe auto-marker is expecting you to submit only your fix_yz function definition. You should not include any calls to your function.

1 Answer

3 votes

Answer:

# the function fix_yz is defined

# it takes a string as parameter

def fix_yz(word):

# new_word is to hold the new corrected string

new_word = ""

# loop through the string

# and check for any instance of y or z.

# if any instance is found, it is replaced accordingly

for each_letter in word:

if each_letter == 'z':

new_word += 'y'

elif each_letter == 'Z':

new_word += 'Y'

elif each_letter == 'y':

new_word += 'z'

elif each_letter == 'Y':

new_word += 'Z'

else:

new_word += each_letter

# the value of new string is returned

return new_word

Step-by-step explanation:

The function is written in Python 3 and it is well commented. An image is attached showing the output of the given example.

The function take a string as input. It then loop through the string and check for any instance of 'y' or 'z'; if any instance is found it is swapped accordingly and then append to the new_word.

The value of bew_word is returned after the loop.

The y and z keys swapping position is messing with your touch typing. You decide to-example-1
User Shaju
by
6.2k points