简体   繁体   English

一旦到达 python 代码中的最后一个字母,如何打破这个递归循环?

[英]How to break this recursive loop once I reach the last letter in my python code?

I would like to break the recursive loop once I reach the last letter of new.一旦到达 new 的最后一个字母,我想打破递归循环。 How can I do this?我怎样才能做到这一点?

Diction is a dictionary. Diction是字典。 The idea here is that I get a word there is an example below and I shrink it by matching the [i:i+1] positions of the word string with the dictionary until it gets down to one letter.这里的想法是我得到一个词,下面有一个例子,我通过将词串的 [i:i+1] 位置与字典匹配来缩小它,直到它缩小到一个字母。

def eval(word):
    diction = {
        'br': 'y',
        'rb': 'y',
        'by': 'r',
        'yb': 'r',
        'yr': 'b',
        'ry': 'b',
        'bb': 'b',
        'yy': 'y',
        'rr': 'r',
    }
    new = ''
    i = 0
    while i < len(word) - 1:
        key1 = word[i:i + 2]
        i += 1
        new += diction[key1]
    return eval(new)


word = 'brybbr'
print(eval(word))

You need to provide the end condition.您需要提供结束条件。

In this code, I added a condition if len(new)==1: to finish the unlimited resursive calls.在这段代码中,我添加了一个条件if len(new)==1:来完成无限的递归调用。

In addition, I also added print(new) inside the eval() instead print(eval(word)) so that it can print new with each calling of eval() .此外,我还在eval()中添加了print(new)而不是print(eval(word)) ,以便它可以在每次调用eval()时打印new

def eval(word):
    diction = {
        'br': 'y',
        'rb': 'y',
        'by': 'r',
        'yb': 'r',
        'yr': 'b',
        'ry': 'b',
        'bb': 'b',
        'yy': 'y',
        'rr': 'r',
    }
    new = ''
    i = 0
    while i < (len(word) - 1):
        key1 = word[i:i + 2]
        i += 1
        new += diction[key1]
    print(new) # to print new for every `eval()` calling.
    if len(new)==1: # to stop endless resursive calling.
        return None
    else:
        return eval(new)


word = 'brybbr'
eval(word)
# ybrby
# ryyr
# byb
# rr
# r
#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当你到达句子中的某个字母时如何打破while循环 - How to break a while loop when you reach a certain letter in a sentence Python:如何中断循环并附加结果的最后一页? - Python: How can I break loop and append the last page of results? 如何摆脱这种递归循环? - How do I break out of this recursive loop? 如何修复我的递归倒计时 python 函数的代码,以便它只打印“LIFT OFF!” 一次? - How do I fix my code for my recursive countdown python function so that it only prints “LIFT OFF!’ once? 如何在 python 中创建一个循环,一旦用户输入退出就会中断? - How do I create a loop in python that will break once user inputs quit? python:while循环没有到达最后一次迭代 - python: while loop does not reach last iteration 如何打破我在Python中的外循环? - How to break my outer loop in Python? 如何在 Python 中创建一个 function 递归地“吃掉”输入字符串的最后一个字母,并在字符串长度为 1 时再次将其吐出? - How do I make a function in Python which recursively "eats" the last letter of the input string and spits it out again once the string has length 1? Python 代码中的最后一个 for 循环总是被忽略 - Python the last for loop in my code is always ignored 刽子手问题:当输入的字母不存在时,如何重置循环?如何让字母在单词中出现多次? - Hangman Issue: How do I make my loop reset when the letter entered is not present and how do I make the letter appear more than once in the word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM