简体   繁体   English

输出不打印,并且没有用 '' 括起来

[英]The output prints none and is not enclosed with ''

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']))

The output is _pp_eNone输出是_pp_eNone

I want my output to be _pp_e while still using the print function to call the function.我希望我的输出是_pp_e同时仍然使用打印函数来调用该函数。

What should I do?我该怎么办?

You can simply modify this to:您可以简单地将其修改为:

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']))

The None is the implicit return value after you have looped through your for loop.在循环完 for 循环后, None是隐式返回值。

Since your function isWordGuessed doesnt have any return keyword, the statement由于您的函数isWordGuessed没有任何return关键字,因此语句

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

Now that is called inside a print method, so None will be returned to print function and it will be printed.现在它在print方法中被调用,所以None将返回到print函数并被打印。

The reason of it occurring at the end of the secretWord is due to usage of end parameter in your print statement inside the isWordGuessed它发生在secretWord末尾的原因是由于在isWordGuessed中的print语句中使用了end参数

PS In python, variable names should be lowercase letters separated by underscores. PS 在python中,变量名应该是小写字母,下划线分隔。 Refer this参考这个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM