什么是return语句的简单基本解释,如何在Python中使用它?
它和print声明有什么区别?
我在运行python脚本时收到此错误:
TypeError: cannot concatenate 'str' and 'NoneType' objects
Run Code Online (Sandbox Code Playgroud)
我很确定'str'意味着字符串,但我不知道'NoneType'对象是什么.我的脚本在第二行开始了,我知道第一行是有效的,因为来自该行的命令在我的asa中,正如我所期望的那样.起初我以为可能是因为我在send_command中使用变量和用户输入.
'CAPS'中的所有内容都是变量,'小写'中的所有内容都是从'parser.add_option'选项输入的.
我正在使用pexpect和optparse
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
send_command(child, SNMPSRVUSRCMD + snmpuser + group + V3AUTHCMD + snmphmac + snmpauth + PRIVCMD + snmpencrypt + snmppriv)
Run Code Online (Sandbox Code Playgroud) 在我之前的问题中,Andrew Jaffe写道:
除了所有其他提示和技巧之外,我认为你错过了一些至关重要的东西:你的功能实际上需要返回一些东西.当你创建
autoparts()或者splittext(),我们的想法是,这将是一个你可以调用的函数,它可以(而且应该)回馈一些东西.一旦你找到了你想要你的函数的输出,你需要把它放在一个return语句中.
def autoparts():
parts_dict = {}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
k, v = line.split()
parts_dict[k] = v
print(parts_dict)
>>> autoparts()
{'part A': 1, 'part B': 2, ...}
Run Code Online (Sandbox Code Playgroud)
此函数创建一个字典,但它不返回任何内容.但是,因为我添加了print,所以当我运行该函数时会显示该函数的输出.什么return东西和print它之间有什么区别?
我已经定义了一个函数如下:
def lyrics():
print "The very first line"
print lyrics()
Run Code Online (Sandbox Code Playgroud)
但是为什么输出会返回None:
The very first line
None
Run Code Online (Sandbox Code Playgroud) 这是我学习python的第一天.所以这对你们许多人来说都是一个noob问题.请参阅以下代码:
#!/usr/bin/env python
import sys
def hello(name):
name = name + '!!!!'
print 'hello', name
def main():
print hello(sys.argv[1])
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
当我运行它
$ ./Python-1.py alice
hello alice!!!!
None
Run Code Online (Sandbox Code Playgroud)
现在,我无法理解它的"None"来源?
这是我的计划
def reverse(letters):
backwards = ""
i = len(letters) - 1
while i >= 0:
backwards = backwards + letters[i]
i = i - 1
print (backwards)
print (reverse("hello"))
Run Code Online (Sandbox Code Playgroud)
它有效,打印出"olleh"但之后,它在新行上打印"None".我问这是为什么.显然程序是反转一个单词,代码工作,没有函数它不打印任何,所以我不知道为什么它在函数中.这是在另一个更大的程序中使用,所以我需要它作为一个函数,因为它是为了学校,我不允许简单地使用.reverse()函数.所以我真的需要修复这些代码而不是大的修改,如果可能的话.
首先,我想为糟糕的头衔道歉,但这是我能做的最好的.我试图拍摄很多截图,希望能让它更容易理解.
所以我正在研究一个不和谐的聊天机器人,现在正在开发一个可以作为待办事项清单的功能.我有一个命令将任务添加到列表中,它们存储在dict中.但是我的问题是以更易读的格式返回列表(见图片).
def show_todo():
for key, value in cal.items():
print(value[0], key)
Run Code Online (Sandbox Code Playgroud)
任务存储在dict被调用的中cal.但是为了让机器人实际发送消息,我需要使用return语句,否则它只是将它打印到控制台而不是实际的聊天(见图片).
def show_todo():
for key, value in cal.items():
return(value[0], key)
Run Code Online (Sandbox Code Playgroud)
以下是我尝试修复它的方法,但由于我使用了返回,for循环无法正常工作.
那么我该如何解决这个问题呢?如何使用return语句将其打印到聊天而不是控制台?
请参阅图片以便更好地理解
我非常简单的 python 函数在最后返回None,我不确定为什么。我看了一些其他的帖子,还是没明白。
这是我的代码:
def printmult(n):
i = 1
while i <= 10:
print (n * i, end = ' ')
i += 1
print(printmult(30))
Run Code Online (Sandbox Code Playgroud) 好的.所以我看到有人使用这个代码,我明白了,所以我将使用它.有必要__init__吗?
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
a = A()
a.method_a('Sailor!')
Run Code Online (Sandbox Code Playgroud)
我不能做这样的事情:
class A(object):
def method_a(self):
print "Hello, Sailor!"
a = A()
print a.method_a()
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我确实得到了"你好,水手!" 但我在它下面有一个"无"?那与此有关__init__吗?有人解释.
规格:Ubuntu 13.04 Python 3.3.1
背景:Python初学者; 搜索了这个问题但我找到的答案更多的是"什么"而不是"为什么";
我打算做什么:创建一个功能,从用户输入测试分数输入,并根据年级比例/曲线输出字母等级; 这是代码:
score = input("Please enter test score: ")
score = int(score)
def letter_grade(score):
if 90 <= score <= 100:
print ("A")
elif 80 <= score <= 89:
print ("B")
elif 70 <= score <= 79:
print("C")
elif 60 <= score <= 69:
print("D")
elif score < 60:
print("F")
print (letter_grade(score))
Run Code Online (Sandbox Code Playgroud)
这在执行时返回:
None
(letter_grade(score)
print (letter_grade(score))
"无"并非意图.而且我发现,如果我使用None而不是None,则"无"不再出现.
我能找到的最接近的答案说"像python中的函数返回None,除非明确指示不这样做".但我确实在最后一行调用了一个函数,所以我在这里有点困惑.
所以我想我的问题是:是什么导致了"无"的消失?我确信这是非常基本的东西,但我无法找到解释"幕后"机制的任何答案.所以,如果有人能对此有所了解,我将不胜感激.谢谢!