Python if len("string") <= 1: 如何打印单词?@codeacademy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16483481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:47:15  来源:igfitidea点击:

Python if len("string") <= 1: how to print the word? @codeacademy

python

提问by Adam Todorovic

print "Welcome to the English to Pig Latin translator!"
original = raw_input ("Type the word you want to translate!")

def Pytranslator():
    if len("string") <= 1:
        print original
    else:
        return "empty"

I want to print the word if it has more than one (1) letter. I've tried

如果单词有超过一 (1) 个字母,我想打印它。我试过了

return True print original

回答by Martijn Pieters

You are testing the length of the wrong thing. Test originalinstead:

你正在测试错误的东西的长度。original改为测试:

if len(original) <= 1:
    print original

len("string")is alwaysgoing to be 6, the number of characters in the value "string":

len("string")始终将是6,在价值的字符数"string"

>>> len("string")
6