Py学习  »  Python

如何让python只在你说“嗨”的时候回复

Nofal • 4 年前 • 726 次点击  

我想先打个招呼,然后你得打个招呼,然后在那儿打个招呼。如果你不打,它会告诉你打个招呼。

hello = input('Hello')

def hiOrHey(hello):
    if hello == 'hi':
        if True:
            print("Hey there!")
    else:
        print("SAY HI")

但是,它不起作用,它只是说“你好!”,并留下一个空行。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54033
 
726 次点击  
文章 [ 3 ]  |  最新文章 4 年前
Dharmveer Singh
Reply   •   1 楼
Dharmveer Singh    4 年前

你在叫你的函数hiOrHey吗。

如果不是,那就叫它。

def hiOrHey(hello): 
    if hello == 'hi':
        print("Hey there!") 
    else: 
        print("SAY HI")
hello = input('Hello')
hiOrHey(hello)
chrischris96
Reply   •   2 楼
chrischris96    4 年前
def hiOrHey(hello):
    print("hello")

    if hello == 'hi':
        print("Hey there!")
    else:
        print("SAY HI")

hiOrHey(hello)

文件名

PacketLoss
Reply   •   3 楼
PacketLoss    4 年前
  1. 你从来不叫你的函数
  2. 你不需要检查 if True 在检查值是否匹配之后,这是多余的。

以下内容将帮助您开始。

def hiOrHey(hello):
    if hello.lower() == 'hi': # Check if hello in lowercase matches hi
        print("Hey there!")
    else:
        print("SAY HI")

hello = input('Say Hi: ')

hiOrHey(hello) # Call your function, passing variable hello