如何使用用户提供的城市并使用它进行回复?使用if

问题描述 投票:-1回答:1

我想让用户问孟买的天气如何,机器人应回复孟买的天气真是太好了

# 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

希望您能理解并为我的格式设置错误感到抱歉:)

python chatbot
1个回答
0
投票

[这可以使用%运算符替换%s占位符,但是此解决方案依赖于传递给chatbot()方法的字符串的最后一个单词,即城市名称

© www.soinside.com 2019 - 2024. All rights reserved.