Python 3 Simon表示要进行循环字符串比较 [英] Python 3 Simon Says For Loop String Comparison

查看:42
本文介绍了Python 3 Simon表示要进行循环字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python 3的新手,我觉得我正在以最糟糕的方式学习.一切都通过称为zybooks的在线教科书进行.我一直在尝试了解循环,对于我应该编写的程序,我必须使用循环.

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.

这里是说明:"Simon说"是一款记忆游戏,其中"Simon"输出10个字符的序列(R,G,B,Y),并且用户必须重复该序列.创建一个比较两个字符串的for循环.对于每个匹配项,将一个点添加到user_score.不匹配时,结束游戏.例如:以下模式产生的user_score为4:

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:

simon模式:R,R,G,B,R,Y,Y,B,G,Y

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

userPattern:R,R,G,B,B,R,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)

问题是什么时候进行第二次测试,我的输出仍然是用户得分:4而不是用户得分:7(第二次测试的simon_pattern和user_pattern的字符串更改了.)

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.)

我知道我需要一次将字符串中的每个元素相互比较并加+1,并且一旦两个元素不匹配,就需要停止循环.我已经尝试过:

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

(然后我继续上面的循环,直到到达[9]并打印user_score,但这也不起作用.)

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

我尝试将len(simon_pattern)与len(user_pattern)进行比较,但这只是抛出一个错误,告诉我它无法执行该功能,因为我有字符串而不是整数.

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.

推荐答案

使用索引会更轻松:

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
    else:
        break

这篇关于Python 3 Simon表示要进行循环字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆