在Python中从另一个函数中调用函数而不使用类

2024-06-02 15:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在从一个函数中调用另一个函数。我知道这个问题在这里被问了很多次,但我找不到正确的答案

下面是一个例子:

使函数shout(word)接受字符串并以大写字母返回该字符串

def shout(word):

    return word.upper()

shout("bob")

创建一个函数improduct(),向用户询问他们的姓名,并向他们大声呼喊。调用函数shout来实现这一点

def introduce():

    name = input("What's your name: ")
    print(f"Hello {name}")

introduce()

我的问题是:在不使用类的情况下,如何从import()func中调用shout()func?结果如下所示:

What's your name?
Bob

HELLO BOB

谢谢你的时间和回答


Tags: 函数字符串答案nameyourreturndef大写字母
3条回答

从其他函数内部调用函数的方式与从外部函数调用函数的方式相同。将函数名放在第一位,将参数放在括号中

def introduce():

    name = input("What's your name: ")
    print(shout(f"Hello {name}"))

您可以在import()中调用shout()函数:

def shout(word):
    return word.upper()

def introduce():
    name = input("What's your name: ")
    print(shout(f"Hello {name}")) << just like this.

只需调用函数shout

def shout(word):
    return word.upper()

def introduce():

    name = input("What's your name: ")
    name = shout(name)
    print(f"Hello {name}")

introduce()

相关问题 更多 >