Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环中的if语句打印两次_Python_Python 3.x - Fatal编程技术网

Python for循环中的if语句打印两次

Python for循环中的if语句打印两次,python,python-3.x,Python,Python 3.x,此函数用于将所有字母转换为小写,并删除所有非字母。我添加了一个打印功能,以查看此代码的工作原理。如上图所示,它会打印“oplk”两次。为什么呢 问题的第二部分,忽略第一部分 def toChars(s): s = s.lower() letters = "" for c in s: if c in 'abcdefghijklmnopqrstuvwxyz': letters = letters + c

此函数用于将所有字母转换为小写,并删除所有非字母。我添加了一个打印功能,以查看此代码的工作原理。如上图所示,它会打印“oplk”两次。为什么呢

问题的第二部分,忽略第一部分

def toChars(s):
    s = s.lower()
    letters = ""
    for c in s:
        if c in 'abcdefghijklmnopqrstuvwxyz':
            letters = letters + c
            print(letters)
    return letters


print(toChars("Op2lk"))

o
op
opl
oplk
oplk
为什么isPal函数中的返回语句不打印任何内容?

您是否考虑过使用“stringtes”.lower()

此外,您已经打印了输出和结果,只需删除函数中的打印:

isPal called with oplk
 About to return False for oplk

isPal called with oppo
 isPal called with pp
 isPal called with 
 About to return True from base case
 About to return True for pp
 About to return True for oppo

在您的代码中,您使用
print(toChars(“Op2lk”)
这是第二个
oplk
是它的输出

如果您想要单线解决方案,以防万一

def toChars(s):
    s = s.lower()
    letters = ""
    for c in s:
        if c in 'abcdefghijklmnopqrstuvwxyz':
            letters = letters + c
    return letters


print(toChars("Op2lk"))

因为您在
打印后使用的是return。

子句的
结束时,它返回
字母
变量。因此,它会返回两次已完成的语句。

这是因为它在循环中正常打印,然后打印返回的字母。

for
循环中的
if
语句没有打印两次。“oplk”的两次重复是由于

  • 方法中的print语句,它在if语句中

  • 打印返回值的方法之外的print语句,即print(toChars(“Op2lk”))


  • 它打印了两次,因为您已经在函数中打印了字母,然后您返回字母并再次在函数外部打印。

    它打印了两次,因为函数每次打印字母,然后您还打印返回值,这是
    字母的最后一个值。
    。当您删除
    打印(字母)时
    从函数中,您只会得到一个输出。这只是暂时的,您可以看到它是如何工作的,不要担心重复。谢谢@Ali baharithanks@Dav_Did HuThanks@Qamar Abbas
    def toChars(s):
        s = s.lower()
        letters = ""
        for c in s:
            if c in 'abcdefghijklmnopqrstuvwxyz':
                letters = letters + c
        return letters
    
    
    print(toChars("Op2lk"))
    
    def toChars(s):
        return ''.join(list(filter(lambda x : ord(x) in range(ord('a'), ord('z')+1), s.lower())))
    
    print(toChars("Op2lk"))