我想让用户问孟买的天气如何,机器人应回复孟买的天气真是太好了
# 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
希望您能理解并为我的格式设置错误感到抱歉:)
[这可以使用%
运算符替换%s
占位符,但是此解决方案依赖于传递给chatbot()
方法的字符串的最后一个单词,即城市名称