사용자가 제공 한 도시를 사용하고 그것을 사용하여 응답하는 방법은 무엇입니까? if 사용

장난

사용자가 뭄바이의 날씨는 어떻냐고 물어보고 봇은 뭄바이의 날씨가 놀랍다 고 답해야합니다.

# i want the %s in the if statement to be blank for user to input his city and ask for weather
def chatbot(n):
if n=='whats the weather in %s': #the user can enter anything in place of %s
    print('the weather in %s is amazing')#%s means like the user enter whats the weather in mumbai

print("BOT: HEY IM CHATBOT MAY I KNOW YOUR NAME?")
a=input('YOU:')
print("BOT: Hi",a.upper())
while True:
print("BOT: ASK ME SOMETHING",a.upper(), "\U0001F642 OR PRESS q TO EXIT!")
n=input("YOU:").lower()
if n != 'q':
    chatbot(n)
else:
    print("BOT: BYE :)")
    break

내 서식 오류를 이해하고 죄송합니다. :)

플로피 52

이것은 %연산자를 사용하여 %s자리 표시자를 대체 하지만이 솔루션은 chatbot()메서드에 전달 된 문자열의 마지막 단어가 도시 이름 인 것에 의존합니다.

def chatbot(n):
    city = n.split(" ")[-1]
    if n=='whats the weather in %s' % city:
        print('the weather in %s is amazing' % city)

chatbot("whats the weather in mumbai")

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관