Respuesta :
import random
letters=["R", "G", "B", "Y"]
i = 0
to_guess = ""
while i < 10:
to_guess += random.choice(letters)
i += 1
print("Simon says "+to_guess)
user_score = 0
user = input()
for x in range(0, len(to_guess)):
try:
if user[x] == to_guess[x]:
user_score += 1
else:
break
except IndexError:
break
print("User score: "+str(user_score))
I hope this helps!
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
for i in range(1,11):
input1 = input1 + random.choice(validletters)
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
for i in range(0,10):
if(input1[i] == input2[i]):
user_score = user_score + 1 This increments user_score by 1 if there is a match
else:
break This ends the program if there is no match
The next three lines print the strings and the user score
print("String: "+input1)
print("Guess: "+input2)
print("User Score: "+str(user_score))