Python 3 Simon Says For Loop String Comparison

Question:

I’m new to python 3 and I feel like I am learning in the worst way possible. Everything is through an online text-book called zybooks. I’ve been trying to understand for loops, and for the program I am supposed to write I have to use for loops.

here are the instructions: “Simon Says” is a memory game where “Simon” outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings. For each match, add one point to user_score. Upon a mismatch, end the game. Ex: The following patterns yield a user_score of 4:

simonPattern: R, R, G, B, R, Y, Y, B, G, Y

userPattern: R, R, G, B, B, R, Y, B, G, Y

to start I am given this:

user_score = 0
simon_pattern = 'RRGBRYYBGY'
user_pattern  = 'RRGBBRYBGY'
print('User score:', user_score)

I have passed the first “test” with this code:

user_score = 0
simon_pattern = 'RRGBRYYBGY'
user_pattern  = 'RRGBBRYBGY'
for simon_pattern in str(simon_pattern):
    for user_pattern in str(user_pattern):
        if str(simon_pattern) == str(user_pattern):
            user_score += 1
            continue
        if str(simon_pattern) != str(user_pattern):
            break
print('User score:', user_score)

the problem is when it goes to do the second test my output is still User score: 4 instead of User score: 7 (the strings for simon_pattern and user_pattern change for the second test.)

I know I need to compare each element in the string to each other one at a time and add +1, and as soon as two elements don’t match my loop needs to stop. I have tried:

user_score = 0
simon_pattern = 'RRGBRYYBGY'
user_pattern  = 'RRGBBRYBGY'
for s in simon_pattern:
    for u in user_pattern:
        if simon_pattern [0] == user_pattern [0]:
            user_score += 1
        if simon_pattern [0] != user_pattern [0]:
            break
        if simon_pattern [1] == user_pattern [1]:
            user_score += 1
        if simon_pattern [1] != user_pattern [1]:
            break

(and then I continue the above loops until I get to [9] and print the user_score, but that doesn’t work, either.)

I’ve tried comparing len(simon_pattern) to len(user_pattern) but that just throws back an error telling me that it can’t perform that function because I have strings and not integers.

I’m wondering if someone can tell me what I’m doing wrong or point me in the right direction. Because at this point I don’t know what I’m doing wrong and why. I’m sorry this is really long, but I wanted to explain as thoroughly as I could. Thank you for your help.

Asked By: Bnyx

||

Answers:

You’d have a much easier time using an index :

user_score = 0
simon_pattern = 'RRGBRYYBGY'
user_pattern  = 'RRGBBRYBGY'
for i in range(len(simon_pattern)):
    if user_pattern[i] == simon_pattern[i]:
        user_score += 1
Answered By: Loïc

The issue with your second attempt code (for s in simon_pattern ...) is that you were comparing every s in simon_pattern to every u in user_pattern). You need to correlate these (usually with an index) so that you only compare the first to the first, second to the second etc.

Answered By: combinatorist

Using index to iterate through each loop you need some number that increases as you go through each iteration. you can just use user_score since it is already assigned to 0, and going to increase after each iteration by 1. You will probably say "but it is going to be increased only if there is a match!" (this is right)! but if there is mismatch you are going to break out of the loop anyways, and end the game so this it’s a perfect.

user_score = 0
simon_pattern = input()
user_pattern  = input()

for i in simon_pattern:
   if user_pattern[user_score] == simon_pattern[user_score]:
      user_score += 1
   else:
      break
            

print('User score:', user_score)
user_score = 0
simon_pattern = input()
user_pattern  = input()

for i in range(len(simon_pattern)):    # loop for each letter in simon_pattern
    if user_pattern[i] == simon_pattern[i]:    # if user matches simon
        user_score += 1    # add 1 to the user_score
    else:    # otherwise exit the game
        break

print('User score:', user_score)    #output final score
Answered By: PanamaPHat

If you need to do it with continues and breaks like I had to, it would be something like this:

user_score = 0
simon_pattern = input()
user_pattern  = input()

for i in range(len(simon_pattern)):
    if user_pattern[i] == simon_pattern[i]:
        user_score += 1
        continue
    else:
        break
print('User score:', user_score)
Answered By: Jonathan Kmieciak
user_score = 0
simon_pattern = input()
user_pattern = input()
x = 0
for n in simon_pattern:
    if n == user_pattern[x]:
        x += 1
        user_score += 1
    else:
        break

print(f'User score: {user_score}')
Answered By: Abdalaziz Eltantawe

the "most useful" answer above is missing the break which will cause the tests to fail. Use the code below to pass all the tests. As many people suggested, using an index is your best bet and gives you the cleanest code as well.

user_score = 0
simon_pattern = input()
user_pattern  = input()
for i in range(len(simon_pattern)):
    if user_pattern[i] == simon_pattern[i]:
        user_score += 1
    else:
        break

print('User score:', user_score)
Answered By: Colleen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.