【问题标题】:How do I stop this from exiting after the first iteration?第一次迭代后如何阻止它退出?
【发布时间】:2021-05-24 22:54:42
【问题描述】:

我试图返回一个用新字符替换给定旧字符的语句,但循环在第一次迭代后继续退出。

def test(s, old_ch, new_ch): 
    """returns the given input and replaces an old character with a new character""" 
    newstring = "" 
    for ch in s: 
        while (ch == old_ch): 
            newstring += new_ch  
            break
        while (ch != old_ch):
            newstring += ch 
            break 
        return newstring

我知道在 python 中已经定义了替换函数,但这是我被告知这样做的方式。 (与for ch in s 位相同)

【问题讨论】:

  • return newstring 不应该在迭代中。删除一个缩进级别。
  • 立即中断的while 循环与if 语句相同。
  • 所以这两个while 循环应该是ifelse

标签: python loops replace iteration


【解决方案1】:

你需要改变两件事。

  1. 不要使用 while 循环和中断,只需使用 if 语句。您的代码仍会以相同的方式工作,但它会更具可读性。

  2. 将 return 语句移到 for 循环之外,即取消缩进。这样newstring 将在整个 for 循环执行完毕之前返回,这是期望的行为。

完整的更正代码如下:

def test(s, old_ch, new_ch): 
    """returns the given input and replaces an old character with a new character""" 
    newstring = "" 
    for ch in s: 
        if (ch == old_ch): 
            newstring += new_ch  
        else:
            newstring += ch 
    return newstring

print(test("Megalovania", "a", "o"))
# Prints Megolovonio

【讨论】:

  • @user15261256 如果我的回答对您有帮助,请考虑接受。这会告诉其他人问题已解决,并帮助人们更轻松地找到正确答案。
【解决方案2】:

for ch in s 语句用于遍历字符串的每个字符。 因此,不需要 for 循环中的 while 循环。

def test(s, old_ch, new_ch): 
    """returns the given input and replaces an old character with a new character""" 
    newstring = "" 
    for ch in s: 
        if(ch == old_ch): 
            newstring = newstring+new_ch  
            
        if(ch != old_ch):
            newstring = newstring + ch 
            
    return newstring

此代码执行您想要完成的预期工作。

【讨论】:

  • 你没有提到你还需要取消缩进return语句。
  • 感谢@M-Chen-3 指出,我忘了提这一点。
【解决方案3】:

试试这个,这样你会更好地理解迭代。

def test(s, old_ch, new_ch): 
    newstring = "" 
    for ch in s:
        print(f"ch is {ch}")
        if (ch == old_ch):
            print(f"if condition is True for ch {ch} and old_ch {old_ch}")
            newstring += new_ch  
        else:
            print(f"elif condition is True for ch {ch} and old_ch {old_ch}")
            newstring += ch
    print(newstring)
    return newstring

调用这个函数

test("New to Python", "t", "y")

输出是

ch is N
elif condition is True for ch N and old_ch t
ch is e
elif condition is True for ch e and old_ch t
ch is w
elif condition is True for ch w and old_ch t
ch is  
elif condition is True for ch   and old_ch t
ch is t
if condition is True for ch t and old_ch t
ch is o
elif condition is True for ch o and old_ch t
ch is  
elif condition is True for ch   and old_ch t
ch is P
elif condition is True for ch P and old_ch t
ch is y
elif condition is True for ch y and old_ch t
ch is t
if condition is True for ch t and old_ch t
ch is h
elif condition is True for ch h and old_ch t
ch is o
elif condition is True for ch o and old_ch t
ch is n
elif condition is True for ch n and old_ch t

【讨论】:

  • 那些打印语句完全没有必要。 Stack Overflow 并不是要取代 Python 教程。
  • 是的,我同意,但对于初学者,我认为它会更有用。
猜你喜欢
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
相关资源
最近更新 更多