Python 3.x Python:使用while循环返回字符串(无方法或for循环)

Python 3.x Python:使用while循环返回字符串(无方法或for循环),python-3.x,while-loop,Python 3.x,While Loop,下面有一个函数,我想返回一个新字符串,其中每个ch都被替换为ch2: def replace(s, ch, ch2): '''(str, str, str) - > str Return a new string where every instance of ch in s is replaced with ch2 Precondition: len(ch) == 1 and len(ch2) == 1 ''' i = 0 m = s[i] while i < len(s):

下面有一个函数,我想返回一个新字符串,其中每个ch都被替换为ch2:

def replace(s, ch, ch2):
'''(str, str, str) - > str
Return a new string where every instance of ch in s is replaced with ch2
Precondition: len(ch) == 1 and len(ch2) == 1
'''

i = 0
m = s[i]
while i < len(s):
    if s[i] == ch:
        return ch2
    m = m + s[i]
    i += 1
return m
我希望输出如下:

'rarrmatarr'
我尝试了几种方法,但只得到了
'r'或'rr'


有人能告诉我哪里出错了吗?

为什么不使用Python中提供的str.replace()

>>> s = 'razzmatazz'
>>> s.replace('z', 'r')
'rarrmatarr'

无需创建自定义函数即可完成所需的操作

为什么不使用Python中提供的str.replace()

>>> s = 'razzmatazz'
>>> s.replace('z', 'r')
'rarrmatarr'

无需创建自定义函数即可完成所需操作

这是该语言的内置功能

'razzmatazz'.replace('z', 'r')

'rarrmatarr'
如果我用你的风格,我会写更多的东西,大致如下:

def replace(s, ch, ch2):
    s2 = list(s)
    i=0
    while i < len(s):
        if s2[i] == ch:
            s2[i]=ch2
        i=i+1
    return "".join(s2)
def更换(s、ch、ch2): s2=列表 i=0 而我这是该语言内置的

'razzmatazz'.replace('z', 'r')

'rarrmatarr'
如果我用你的风格,我会写更多的东西,大致如下:

def replace(s, ch, ch2):
    s2 = list(s)
    i=0
    while i < len(s):
        if s2[i] == ch:
            s2[i]=ch2
        i=i+1
    return "".join(s2)
def更换(s、ch、ch2): s2=列表 i=0 而我
return
立即返回值并退出函数,即使它处于循环中。您可能想摆脱
while
循环,而使用一个漂亮的
for
循环:

def replace(s, ch, ch2):
    '''(str, str, str) - > str
    Return a new string where every instance of ch in s is replaced with ch2
    Precondition: len(ch) == 1 and len(ch2) == 1
    '''

    result = ''

    for char in s:
        if char == ch:
            result += ch2
        else:
            result += char

    return result
尽管使用内置的
str.replace
方法会更好:

def replace(s, ch, ch2):
    return s.replace(ch, ch2)
或者更简洁地说:

replace = str.replace

return
立即返回值并退出函数,即使它处于循环中。您可能想摆脱
while
循环,而使用一个漂亮的
for
循环:

def replace(s, ch, ch2):
    '''(str, str, str) - > str
    Return a new string where every instance of ch in s is replaced with ch2
    Precondition: len(ch) == 1 and len(ch2) == 1
    '''

    result = ''

    for char in s:
        if char == ch:
            result += ch2
        else:
            result += char

    return result
尽管使用内置的
str.replace
方法会更好:

def replace(s, ch, ch2):
    return s.replace(ch, ch2)
或者更简洁地说:

replace = str.replace

我认为你的代码应该是

i = 0
m = s[i]
while i < len(s):
    if s[i] == ch:
        m = m + ch2 // Here you are lagging.
     else   
        m = m + s[i]
    i += 1
return m
i=0
m=s[i]
而我
因为,在您的代码中,当在字符串
razzmatazz
中时,如果第一个
z
匹配,则不替换。,
它返回
ch2
,即
r
。因此,您得到了
r

我认为您的代码应该是

i = 0
m = s[i]
while i < len(s):
    if s[i] == ch:
        m = m + ch2 // Here you are lagging.
     else   
        m = m + s[i]
    i += 1
return m
i=0
m=s[i]
而我
因为,在您的代码中,当在字符串
razzmatazz
中时,如果第一个
z
匹配,则不替换。,
它返回
ch2
,即
r
。因此,当您在原始字符串中找到
ch
时,您将从函数中得到
r
您将返回
ch2
,因此这无法按您期望的方式工作。您必须向新字符串中添加正确的字符,然后在迭代原始字符串中的每个字符后返回该字符串

i = 0
m = ''
while i < len(s):
    m += ch2 if s[i] == ch else s[i]
    i += 1

return m
i=0
m=''
而我

另外,正如其他答案所指出的,有更好的方法来实现这一点。

当您在原始字符串中找到
ch
时,您将从函数返回
ch2
,因此这无法按您期望的方式工作。您必须向新字符串中添加正确的字符,然后在迭代原始字符串中的每个字符后返回该字符串

i = 0
m = ''
while i < len(s):
    m += ch2 if s[i] == ch else s[i]
    i += 1

return m
i=0
m=''
而我

另外,正如其他答案所指出的,有更好的方法来实现这一点。

str有一个名为
replace
的方法,它可以满足您的需要。有什么特别的原因需要你自己去实现吗?它们是给我的练习题,我的导师特别告诉我们不要使用方法或循环。另外,我只想学习如何使用while循环,因为我仍然不习惯使用它们。实际上,您使用的方法是:
len(string)=>string.\uu len(string)(
string[I]=>string.\uu getitem\uui
,等等。str有一个名为
replace
的方法,可以满足您的需要。有什么特别的原因需要你自己去实现吗?它们是给我的练习题,我的导师特别告诉我们不要使用方法或循环。另外,我只想学习如何使用while循环,因为我仍然不习惯使用它们。实际上,您正在使用方法:
len(string)=>string.\uu len(string)(
string[I]=>string.\uu getitem\uui
,等等。原因是我们被明确告知不要使用方法。另外,我想学习如何更好地使用while循环。我在实现它以获得期望的结果方面仍然有一些困难。明白。正如大多数人指出的,您需要将return语句移出循环,因为它将返回在该点上具有的任何值,并用它取消循环执行。另外,我想学习如何更好地使用while循环。我在实现它以获得期望的结果方面仍然有一些困难。明白。正如大多数人指出的,您需要将return语句移出循环,因为它将返回在该点上具有的任何值,并用它取消循环执行。我想学习如何更好地使用while循环。在某些情况下,我仍然难以实现它。另外,有人告诉我不应该在练习问题中使用方法。@user2489861。好吧看看我的更新。主要的问题是你在第一场比赛后就回来了。我想学习如何更好地使用while循环。在某些情况下,我仍然难以实现它。另外,有人告诉我不应该在练习问题中使用方法。@user2489861。好吧看看我的更新。主要问题是您在第一次matc之后返回