Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python完成此函数,然后将其打印出来?_Python_Reverse - Fatal编程技术网

如何使用Python完成此函数,然后将其打印出来?

如何使用Python完成此函数,然后将其打印出来?,python,reverse,Python,Reverse,我很难理解如何使用函数——那时我可以做,但之后我就不知道如何使用它们了。我的问题是如何用函数打印此代码 string = "Hello" reverse = string[::-1] print(reverse) 我试着把它放在一个函数中,但我不能让它打印Hello def reverse_a_string(string): string = "Hello" reverse = string[::-1] print(reverse) 我也试过这个 def revers

我很难理解如何使用函数——那时我可以做,但之后我就不知道如何使用它们了。我的问题是如何用函数打印此代码

string = "Hello"
reverse = string[::-1]
print(reverse)
我试着把它放在一个函数中,但我不能让它打印Hello

def reverse_a_string(string):
    string = "Hello"
    reverse = string[::-1]
    print(reverse)
我也试过这个

def reverse_a_string(string):
    string = "Hello"
    reverse = string[::-1]

print(reverse)
似乎什么都不管用。我也有同样的问题

total = 0

def length(words):

    for i in words:
        total += 1
    return total

我不知道您是在问如何在python中定义函数还是其他什么

如果您想学习python函数,请访问或在google中编写python教程,您将获得数十亿个好站点

def reverse_a_string(string): 
     #function definition
     reverse = string[::-1] 
     print(reverse)

#function call
reverse_a_string("your string")
但若要为此定义函数,只需

   print( string[::-1] )

我不知道您是在问如何在python中定义函数还是其他什么

如果您想学习python函数,请访问或在google中编写python教程,您将获得数十亿个好站点

def reverse_a_string(string): 
     #function definition
     reverse = string[::-1] 
     print(reverse)

#function call
reverse_a_string("your string")
但若要为此定义函数,只需

   print( string[::-1] )
对于函数,您必须定义函数,然后用函数名调用它,即funtion()

在我的示例中,我请求一个字符串,将其分配给变量,并在函数中使用它。如果您只想打印hello(我对您的问题有点不清楚),那么只需在函数内部包含print(“hello”)或w/变量print(string)即可

对于函数,您必须定义函数,然后用函数名调用它,即funtion()


在我的示例中,我请求一个字符串,将其分配给变量,并在函数中使用它。如果您只想打印hello(我对您的问题有点不清楚),那么只需在函数内部包含print(“hello”)或w/变量print(string)即可。

创建函数后,必须调用它。您已经创建了函数
reverse\u a\u string
,但是您从未真正调用过它。把一个函数想象成一个按钮,它在每次按下(或者在我们的例子中是调用)时都会做一些事情。如果你从未按下按钮,那么尽管它有潜力做某事,但它永远不会。为了让这组指令发生,我们需要按下按钮(或者在我们的例子中调用函数)。因此,为了让代码正常工作,您首先需要定义函数,然后实际调用它:

def reverse_a_string():
    string="Hello"
    reverse = string[::-1]
    print reverse
反转字符串()

结果:“奥利”

如果您希望将自己的字符串传递给函数,使其不只是在代码需要显示的所有时间返回“olleH”:

def reverse_a_string(stringThatWillBeReversed):
    reverse = stringThatWillBeReversed[::-1]
    print reverse

reverse_a_string('whateverStringYouWant')
结果:与您输入的字符串相反


希望有帮助

创建函数后,必须调用它。您已经创建了函数
reverse\u a\u string
,但是您从未真正调用过它。把一个函数想象成一个按钮,它在每次按下(或者在我们的例子中是调用)时都会做一些事情。如果你从未按下按钮,那么尽管它有潜力做某事,但它永远不会。为了让这组指令发生,我们需要按下按钮(或者在我们的例子中调用函数)。因此,为了让代码正常工作,您首先需要定义函数,然后实际调用它:

def reverse_a_string():
    string="Hello"
    reverse = string[::-1]
    print reverse
反转字符串()

结果:“奥利”

如果您希望将自己的字符串传递给函数,使其不只是在代码需要显示的所有时间返回“olleH”:

def reverse_a_string(stringThatWillBeReversed):
    reverse = stringThatWillBeReversed[::-1]
    print reverse

reverse_a_string('whateverStringYouWant')
结果:与您输入的字符串相反

希望有帮助

没有返回值的函数 只执行操作或执行某些操作而不返回值的函数(例如,
print
)。
不返回值的函数可以这样定义:

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
可以这样使用(称为):

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
输出将是:

你好!

功能参数 函数还可以从调用方接收参数(变量类型)。最好用一个例子来说明。
接收名称并向该名称致意的函数:

def sayHelloTo(name):
    print "Hello", name
可以这样称呼:

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
输出将是:

你好,尤塔姆

参数是函数的输入

具有返回值的函数 其他函数不同于
sayHello()
sayHelloTo(name)
(只执行某些操作)可以返回值。例如,让我们创建一个掷骰子的函数(返回1到6之间的随机数)。 从随机导入randint

def rollDice():
    result = randint(1, 6)
    return result
return
关键字只设置函数的
输出值
,然后退出函数。
rollDice
功能的示例使用如下:

dice = rollDice()
print "The dice says", dice
当函数点击一个
return
关键字时,它将结束,返回值(在我们的例子中,变量
result
)将被放置,而不是函数调用。让我们假设
randint(1,6)
产生了数字3。
结果变为3。
返回结果。
现在,代替这一行:

dice = rollDice()
我们可以将该行视为:

dice = 3
rollDice()
已替换为
3

具有参数和返回值的函数 某些函数(例如,数学函数)可以接受输入并生成输出。例如,让我们创建一个函数,该函数接收2个数字并输出较大的数字

def max(a,b):
    if a > b:
        return a
    else:
        return b
它的作用很清楚,不是吗?如果
a
较大,则返回其值。否则,返回
b


它可以这样使用:

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
输出将是:

六,

现在,你的案子 您要做的是一个反转字符串的函数。它应该接受1个参数(输入)-要反转的字符串,并输出1个值-反转的字符串。这可以通过以下方式实现:

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
现在,您可以这样做:

def sayHello():
    print "Hello!"
sayHello()
sayHelloTo("Yotam")
print max(4, 6)
def reverse_a_string(my_text):
    return my_text[::-1]
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
    global reverse #Stating that we are going to use the global variable reverse
    reverse = string[::-1]

# Then you can call it like that:
reverse_a_string("Hello")
print reverse
r将包含s的反向值,并将被打印。

关于你的第二个功能——好吧,我假设根据这个答案,你可以自己做,但是如果你需要第二个功能的帮助,请评论我

局部变量 关于第三个示例:

def reverse_a_string(string):
    string = "Hello"
    reverse = string[::-1]

print(reverse)