【问题标题】:With this code, how would I be able to print the value of the function, "shout"? [duplicate]使用此代码,我将如何打印函数的值,\"shout\"? [复制]
【发布时间】:2022-08-17 10:07:10
【问题描述】:
def shout(phrase):
    if phrase == phrase.upper():
        return (\"YOU\'RE TOO LOUD\")
    else:
        return (\"Can you speak up, I can barely hear you\")

shout(\"I\'M INTERESTED IN SHOUTING\")
  • 另外,请不要对此投反对票。我已经不得不等待 2 周才能发布这个问题,因为它“没有受到社区的好评。”
  • 请阅读How to Askmeta.stackoverflow.com/questions/261592,并在发布问题之前尝试查找现有答案。问题之所以被否决,是因为它们被认为对网站没有用。请记住,这是不是讨论区;问题应该有助于建立一个有组织的、可搜索的参考图书馆。

标签: python


【解决方案1】:

您只需要将函数调用包装在 print 语句中。

def shout(phrase): 
    if phrase == phrase.upper(): 
        return ("YOU'RE TOO LOUD") 
    else: 
        return ("Can you speak up, I can barely hear you")

print(shout('hello'))
print(shout('HELLO'))

# Can you speak up, I can barely hear you
# YOU'RE TOO LOUD

【讨论】:

    【解决方案2】:

    用 print() 函数替换 return

    def shout(phrase):
        if phrase == phrase.upper():
            print("YOU'RE TOO LOUD")
        else:
            print("Can you speak up, I can barely hear you")
    
    shout("I'M INTERESTED IN SHOUTING")
    

    【讨论】:

    • 为什么反对票任何人都可以解释我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2022-01-22
    • 2017-12-01
    • 2014-04-08
    • 2018-08-28
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多