简体   繁体   English

Python:打印函数结果将返回“ None”

[英]Python: Printing the result of a function returns “None”

I have some python code, the translate_string method returns the correct value nevmban but then after it prints "None". 我有一些python代码,translate_string方法返回正确的值nevmban但随后它显示“ None”。

def secret_translate(letter):
    code = ord(letter)-ord('a')
    code = (code+13)%26
    newCode = chr(code+ord('a'))
    return newCode

def translate_string(string):

    newMsg = ''
    i = 0
    while i < (len(string)):

        print(secret_translate(string[i]),end='')
        i= i+1 

print(translate_string("arizona"))

The program prints: 程序打印:

nevmbanNone

I expect it to print nevmban Where is the "None" coming from? 我希望它能打印nevmban “无”来自哪里?

由于没有显式的return语句,因此您正在打印translate_string函数的结果,即None

When you call the print method like this: 当您像这样调用print方法时:

print(underground_code(secret[i]),end='')

You are passing just one character in the function instead of the whole string. 您只在函数中传递了一个字符,而不是整个字符串。 It will keep on checking the first character in your while loop: 它将继续检查while循环中的第一个字符:

while i < (len(secret)):

The above while will run only once. 上面的while将只运行一次。 And the next function call your len(secret) will be 1. 下一个调用您的len(secret)函数将为1。

*Of course, ignore what I said if this is what you want. *当然,如果您要这样做,请忽略我说的话。

Note that if you have the translation table, you really don't need to implement translation mechanisms. 请注意,如果您有转换表,则实际上不需要实现转换机制。 You have them built in string : 您已经将它们内置在string

import string

fromlist="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
tolist = "cdefghijklmnopqrstuvwxyzab"
transtable = string.maketrans(fromlist, tolist)

mycypertext="FCJJM UMPJB"
print string.translate(mycyphertext, transtable) #will print hello world

You print the letters of the translated word as you go, I would recommend returning the translated word from the function instead. 您可以随时打印已翻译单词的字母,我建议您从函数中返回已翻译单词。 The fact that you see None printed behind your translated word is because inside the function you print the word, and outside the function you print the result of the function. 之所以看到“已翻译”字词后面显示“无”,是因为您在该函数内部打印了该单词,而在函数外部打印了该函数的结果。 The function has no result (no return statement) and the default is None. 该函数没有结果(没有return语句),默认值为None。 Printing this leads to the None being appended at the end of the string that was already printed inside the function. 打印此行将导致在已在函数内部打印的字符串的末尾附加“无”。

I would recommend a restructuring of translate_string like this: 我建议像下面这样对translate_string进行重组:

def translate_string(string):
  translated_letters = [secret_translate(letter) for letter in string]
  return("".join(translated_letters))

trans_string = translate_string("arizona")
print(trans_string)
'nevmban'

This solution uses a list comprehension to loop over the letters of the input string, no need for an explicit loop with a counter. 此解决方案使用列表推导来循环输入字符串的字母,而无需使用计数器的显式循环。 List comprehensions are very nice, they do need some getting used to. 列表理解非常好,他们确实需要一些习惯。 In addition, the function now returns the string instead of printing it inside the function. 另外,该函数现在返回字符串,而不是在函数内部打印它。 The None is gone, and you can use the translated string further along in your program, eg as input to another function. None消失了,您可以在程序中进一步使用转换后的字符串,例如,作为另一个函数的输入。

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

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