203k views
1 vote
Ask a user to repeatedly enter the two stings. Keep asking the user to enter new strings until both stings have a length greater than 0 . Use function len() to find the length of a string. Check if two strings have the same length (the same number of letters/characters in the string) - If they are not the same, print 'Strings do not match in length' and prompt the user to enter the words again. Have them enter both strings again (it may be helpful to ask the user to enter the strings of the same length) - If they have the same length - Use nested loops to iterate through both strings - Compare each letter in the first string to each letter in the second string - If a letter of the first string is in the second string and in the same spot, write: ' w is in the input 2 string and in the correct spot 1 ' where w is the letter you are checking and 1 is the position in both strings Print 'Correct letters in correct spot: X ' where X is the number of correct letters in the correct spot discovered so far. - If a letter of the first string is in the second string but in a different spot, write: ' o is in the input_2 string but not in the correct spot' - If all the letters in both strings are the same, write: 'Strings are the same' How will you determine this case? Use only while loop, for loop, if (if-else, if-elif-else) statement, !=, =, print (, len( ) and range() functions. Example \#1: Example \#3 If the inputs are The output is: You entered 2 1-letter strings which will be followed by two prompts Enter string 1: and Enter string 2: ... Example \#4 If the inputs are mochi mochi The output is: You entered 2 5-letter strings m is in the input 2 string and in the correct spot 1 Correct letters in the correct spot: 1 o is in the input 2 string and in the correct spot 2 Correct letters in the correct spot: 2 c is in the input 2 string and in the correct spot 3 Correct letters in the correct spot: 3 h is in the input 2 string and in the correct spot 4 Correct letters in the correct spot: 4 i is in the input 2 string and in the correct spot 5 Correct letters in the correct spot: 5 The strings are the same which will be followed by two prompts Enter string 1: and Enter string 2:… Example \#5 If the inputs are The output is: You entered 2 2-letter strings a is in the input 2 string and in the correct spot 1 Correct letters in the correct spot: 1 a is in the input 2 string but not in the correct spot which will be followed by two prompts Enter string 1 : and Enter string 2: ... Hint: To iterate through a string, you can use range and indexing as shown below: my_string = 'Mochi' for i in range(len(my_string)): print(my_string[i])

User Adventune
by
7.6k points

1 Answer

5 votes

Final answer:

To solve this problem, you can use a while loop to repeatedly ask the user for input until both strings have a length greater than 0.

Step-by-step explanation:

To solve this problem, you can use a while loop to repeatedly ask the user for input until both strings have a length greater than 0. You can use the len() function to find the length of a string. Then, you can check if the two strings have the same length using an if statement. If they do not have the same length, you can print 'Strings do not match in length' and prompt the user to enter both strings again.

If the strings have the same length, you can use nested loops to iterate through both strings. Inside the nested loops, you can compare each letter in the first string to each letter in the second string. If a letter of the first string is in the second string and in the same spot, you can print 'w is in the input 2 string and in the correct spot 1', where w is the letter you are checking and 1 is the position in both strings.

You can keep track of the number of correct letters in the correct spot by using a variable. You can print 'Correct letters in correct spot: X', where X is the number of correct letters in the correct spot discovered so far. If a letter of the first string is in the second string but in a different spot, you can print 'o is in the input_2 string but not in the correct spot'.

If all the letters in both strings are the same, you can print 'Strings are the same'.

User CopyOfA
by
8.2k points