Python 输出不打印任何内容,且不包含'';

Python 输出不打印任何内容,且不包含'';,python,python-3.x,function,printing,return,Python,Python 3.x,Function,Printing,Return,输出为\u pp\u eNone 我希望我的输出是\u pp\u e,同时仍使用print函数调用该函数 我该怎么办?您只需将其修改为: def isWordGuessed(secretWord, lettersGuessed): for char in secretWord: if char not in lettersGuessed: print('_',end=' ') else: print(cha

输出为
\u pp\u eNone

我希望我的输出是
\u pp\u e
,同时仍使用print函数调用该函数


我该怎么办?

您只需将其修改为:

def isWordGuessed(secretWord, lettersGuessed):
    for char in secretWord:

        if char not in lettersGuessed:
            print('_',end=' ')
        else:
            print(char,end=' ')

print(isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's']))

None
是通过for循环后的隐式返回值。

由于函数
是WordGuessed
没有任何
return
关键字,因此语句

isWordGuessed('apple',['e','i','k','p','r','s'])
将返回
None

现在它在
print
方法中被调用,因此
None
将返回到
print
函数并被打印

出现在
secretWord
末尾的原因是在
isWordGuessed
中的
print
语句中使用了
end
参数

附言。
在python中,变量名应该是用下划线分隔的小写字母。请参阅

可能的副本,抱歉所有的困惑。我30秒前就加入了这个网站。好的,我为您修复了它。当我按现在的方式运行代码时,我得到了
\upp\ue
。你期待什么不同的吗?你是怎么做到的。另外,我想知道你是否可以在使用打印时得到“_pp_e”(iWordGuessed('apple',[e',[I','k','p','r',[s'))谢谢你。但是还有其他方法解决这个问题吗?是的,当然。您可以在循环后添加一个
返回“
。这将解决在完成for循环后返回
None
的问题。
def isWordGuessed(secretWord, lettersGuessed):
     for char in secretWord:
         if char not in lettersGuessed:
             print("_", end="")
         else:
             print(char, end="")
     return ""

print(isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's']))