ValueError: too many values to unpack while trying to get a callback message from a chatbot
ValueError: too many values to unpack while trying to get a callback message from a chatbot
在使用 python 和尝试通过查找 datacamp/others 中的基本函数定义技术来构建聊天机器人方面,我是初学者。
但是,当我尝试调用我的函数定义之一时出现此错误。错误在 "response, phrase = match_rule(rules,message)" 上。有人可以帮我解决这个问题吗?
rules ={'I want (.*)': ['What would it mean if you got {0}',
'Why do you want {0}',
"What's stopping you from getting {0}"]}
import re
import random
def match_rule(rules, message):
response, phrase = "default", None
# Iterate over the rules dictionary
for pattern, responses in rules.iteritems():
# Create a match object
match = re.search(pattern,message)
if match is not None:
# Choose a random response
response = random.choice(responses)
if '{0}' in response:
phrase = match.group(1)
# Return the response and phrase
return response.format(phrase)
def replace_pronouns(message):
message = message.lower()
if 'me' in message:
# Replace 'me' with 'you'
return re.sub('me','you',message)
return message
def respond(message):
# Call match_rule
response, phrase = match_rule(rules,message)
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = replace_pronouns(phrase)
# Include the phrase in the response
response = response.format(phrase)
return response
respond("I want a good code")
收到错误:
ValueErrorTraceback (most recent call last)
<ipython-input-27-e6efc4eacb15> in <module>()
----> 1 print(respond("I want a good code"))
<ipython-input-21-4715e10175ce> in respond(message)
1 def respond(message):
2 # Call match_rule
----> 3 response, phrase = match_rule(rules,message)
4 if '{0}' in response:
5 # Replace the pronouns in the phrase
ValueError: too many values to unpack
欢迎使用 Whosebug。
您的问题在这里:
def respond(message):
# Call match_rule
print(f"{match_rule(rules,message)}")
print("----")
response, phrase = match_rule(rules,message) # line with the issue
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = replace_pronouns(phrase)
# Include the phrase in the response
response = response.format(phrase)
return response
如果你调用它,你的 match_rule(rules, message)
的 return 值为:
What's stopping you from getting a good code
它 return 是一个值,但您正试图通过编码解包两个值:
response, phrase = match_rule(rules,message)
要么将 match_rule
的 return 更改为 return 两个值,要么使用变量 response
或 phrase
之一,而不是两者。
在使用 python 和尝试通过查找 datacamp/others 中的基本函数定义技术来构建聊天机器人方面,我是初学者。 但是,当我尝试调用我的函数定义之一时出现此错误。错误在 "response, phrase = match_rule(rules,message)" 上。有人可以帮我解决这个问题吗?
rules ={'I want (.*)': ['What would it mean if you got {0}',
'Why do you want {0}',
"What's stopping you from getting {0}"]}
import re
import random
def match_rule(rules, message):
response, phrase = "default", None
# Iterate over the rules dictionary
for pattern, responses in rules.iteritems():
# Create a match object
match = re.search(pattern,message)
if match is not None:
# Choose a random response
response = random.choice(responses)
if '{0}' in response:
phrase = match.group(1)
# Return the response and phrase
return response.format(phrase)
def replace_pronouns(message):
message = message.lower()
if 'me' in message:
# Replace 'me' with 'you'
return re.sub('me','you',message)
return message
def respond(message):
# Call match_rule
response, phrase = match_rule(rules,message)
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = replace_pronouns(phrase)
# Include the phrase in the response
response = response.format(phrase)
return response
respond("I want a good code")
收到错误:
ValueErrorTraceback (most recent call last)
<ipython-input-27-e6efc4eacb15> in <module>()
----> 1 print(respond("I want a good code"))
<ipython-input-21-4715e10175ce> in respond(message)
1 def respond(message):
2 # Call match_rule
----> 3 response, phrase = match_rule(rules,message)
4 if '{0}' in response:
5 # Replace the pronouns in the phrase
ValueError: too many values to unpack
欢迎使用 Whosebug。
您的问题在这里:
def respond(message):
# Call match_rule
print(f"{match_rule(rules,message)}")
print("----")
response, phrase = match_rule(rules,message) # line with the issue
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = replace_pronouns(phrase)
# Include the phrase in the response
response = response.format(phrase)
return response
如果你调用它,你的 match_rule(rules, message)
的 return 值为:
What's stopping you from getting a good code
它 return 是一个值,但您正试图通过编码解包两个值:
response, phrase = match_rule(rules,message)
要么将 match_rule
的 return 更改为 return 两个值,要么使用变量 response
或 phrase
之一,而不是两者。