Answer:
import random
validletters=["R", "G", "B", "Y"]
input1 = ""
for i in range(1,11):
input1 = input1 + random.choice(validletters)
input2 = input("Input String 2: ")
if len(input2) != 10:
print("Invalid Length")
else:
user_score = 0
for i in range(0,10):
if(input1[i] == input2[i]):
user_score = user_score + 1
else:
break
print("String: "+input1)
print("Guess: "+input2)
print("User Score: "+str(user_score))
Explanation:
The code assumes that user input will always be R, G, B or Y
The next two lines get two string user inputs
import random
This lists the valid letters
validletters=["R", "G", "B", "Y"]
This initializes an empty string
input1 = ""
The following iteration generates the input string of 10 characters
<em>for i in range(1,11):
</em>
<em> input1 = input1 + random.choice(validletters)
</em>
This prompts user for input string
input2 = input("Input String 2: ")
This checks if input string is up to length of 10
if len(input2) != 10:
If yes, it prints invalid length
print("Invalid Length")
else:
If otherwise, user_score is initialized to 0
user_score = 0
The following iteration checks for matching characters in both strings
<em> for i in range(0,10):
</em>
<em> if(input1[i] == input2[i]):
</em>
<em> user_score = user_score + 1 </em><em>This increments user_score by 1 if there is a match</em>
<em> else:
</em>
<em> break </em><em>This ends the program if there is no match</em>
The next three lines print the strings and the user score
print("String: "+input1)
print("Guess: "+input2)
print("User Score: "+str(user_score))