+ 1

Python HELP

ValueError: too many values to unpack (expected 2) 'near bottom' def match_rule(rules, message): response, phrase = "default", None for pattern, responses in rules.items(): match = re.search(pattern, message) if match is not None: response = random.choice(responses) if '{0}' in response: phrase = match.group(1) return response.format(phrase) def replace_pronouns(message): message = message.lower() if 'me' in message: return re.sub('me', 'you', message) if 'my' in message: return re.sub('my', 'your', message) if 'your' in message: return re.sub('your', 'my', message) if 'you' in message: return re.sub('you', 'me', message) return message def respond(message): response, phrase = match_rule(rules, message) ##This is the error line## if '{0}' in response: phrase = replace_pronouns(phrase) response = response.format(phrase) return response

14th Jan 2020, 7:42 AM
Olivia
Olivia - avatar
17 ответов
+ 2
Ok, please re-check the code; I've amended it. On line 58, I've changed response.format(phrase) to just response. The effect response.format(phrase) had on the string was to put the phrase string in place of the '{0}' in the response string. The trouble came when the if statement on line 76 checked for the '{0}', didn't find it and thus, didn't run the replace_pronouns() function. Maybe I should have picked up on this sooner, so sorry about that. I only realised this was happening after looking more deeply into your code and how it worked. Good job though! I like how you've done it 👍🏻
23rd Feb 2020, 9:53 AM
Russ
Russ - avatar
+ 1
It looks like your line: response, message = match_rule(rules, message) is expecting two outputs from the match_rule function, but its return value is just one (string) value. It's attempting to unpack one string to two variables, that's the error.
18th Jan 2020, 3:38 PM
Russ
Russ - avatar
+ 1
I'm a newb could you explain this a little more and how to fix it?
19th Jan 2020, 12:04 PM
Olivia
Olivia - avatar
+ 1
Sure. Consider the following lone of code. a, b, c = [1, 2, 3] Here, a will be assigned the value of 1, b will be assigned the value of 2 and c will be assigned thw value of 3. This is known is 'unpacking'. The problem you have is that you have more variables being assigned (i.e. on the left hand side of the '=') than you have values to assign to them. Effectively, your code does this: a, b = 'Hello' That means there aren't enough values on the right hand side to assign to the variables you have on the left. Your match_rule() function returns a single string but the line where the problem is is trying to assign it to two variables. Hopefully you can see how to amend the issue. If not I'll have to pick through it and see how to change it when I have some time.
19th Jan 2020, 8:01 PM
Russ
Russ - avatar
+ 1
I understand the whole variable matching variable thing, I just don't see how that's happend with my code...surely response(1), phrase(2) = match_rule(rules(1), message(2))?
20th Jan 2020, 9:05 AM
Olivia
Olivia - avatar
+ 1
I see your confusion. Your match_rule() function is certainly taking two arguments, but the thing is that it only *returns* one value. The return line of that function; return response.format(phrase) means that it returns just a string (response.formst(phrase) is a string). This is how the problem comes about. Does that make sense?
20th Jan 2020, 11:09 AM
Russ
Russ - avatar
+ 1
Thank you so much, I'm going to try getting rid of that line and see if it works. I'll let you know the result :)
22nd Jan 2020, 10:30 AM
Olivia
Olivia - avatar
+ 1
Deleting that line gave me another error unfortunately, cannot unpack non-iterable NoneType object... (On the same problem line)
22nd Jan 2020, 10:36 AM
Olivia
Olivia - avatar
+ 1
Wait, did you delete the return line of your match_rule() function? If so then that would be why you have that error. Now the function is not returning anything, the line response, phrase = match_rule(...) means that you are trying to unpack nothing to two variables. Would you be willing to share your complete code to this thread? It would make it much easier to test/debug it if I could see it all.
22nd Jan 2020, 1:07 PM
Russ
Russ - avatar
+ 1
import random import re bot_template = "BOT : {0}" user_template = "USER : {0}" ##name = "Bot" ##weather = "cloudy" ##responses = { ## "what's your name?" : [ ## "my name is {0}".format(name), ## "they call me {0}".format(name), ## "I like to go by {0}".format(name) ## ], ## "what's today's weather?" : [ ## "the weather is {0}".format(weather), ## "it looks {0} outside".format(weather), ## "it's {0} today".format(weather) ## ], ## "default" : "default message" ## } rules = {'I want (.*)': ['What would it mean if you got {0}', 'Why do you want {0}', "What's stopping you from getting {0}"], 'do you remember (.*)': ['Did you think I would forget {0}', "Why haven't you been able to forget {0}", 'What about {0}', 'Yes .. and?'], 'do you think (.*)': ['if {0}? Absolutely.', 'No chance'], 'if (.*)': ["Do you really think it's likely that {0}", 'Do you wish that {0}', 'What do you think about {0}', 'Really--if {0}']} def send_message(message): print(user_template.format(message)) response = respond(message) print(bot_template.format(response)) ##def respond(message): ## if message in responses: ## bot_message = random.choice(responses[message]) ## else: ## bot_message = random.choice(responses[default]) ## return bot_message def match_rule(rules, message): response, phrase = "default", None for pattern, responses in rules.items(): match = re.search(pattern, message) if match is not None: response = random.choice(responses) if '{0}' in response: phrase = match.group(1) #return response.format(phrase)
13th Feb 2020, 12:23 PM
Olivia
Olivia - avatar
+ 1
def replace_pronouns(message): message = message.lower() if 'me' in message: return re.sub('me', 'you', message) if 'my' in message: return re.sub('my', 'your', message) if 'your' in message: return re.sub('your', 'my', message) if 'you' in message: return re.sub('you', 'me', message) return message def respond(message): response, phrase = match_rule(rules, message) if '{0}' in response: phrase = replace_pronouns(phrase) response = response.format(phrase) return response
13th Feb 2020, 12:24 PM
Olivia
Olivia - avatar
+ 1
Thanks. I've had a quick look and here's what I've done so far... https://code.sololearn.com/chAYGq7gEQrS/?ref=app On line 81, I've added the function call so that the code actually prints some output, and I altered line 58 so that the return line now returns two strings rather than just the one. This means that the unpacking is even on both sides 😊 Hope that clears up your confusion. Let me know if you need any further help 👍🏻
13th Feb 2020, 2:28 PM
Russ
Russ - avatar
+ 1
Thank you, Ive changed line 58. I no longer get the value error but for some reason my change pronoun function is no longer working...
22nd Feb 2020, 11:15 AM
Olivia
Olivia - avatar
+ 1
Just a thought; I added a second input line to make you aware of a possible flaw in your code. A word like 'some' will get changed to 'soyou'. In addition to that, once any pronoun is found in response, it will not check for any further pronouns since you use return after each check.
23rd Feb 2020, 10:00 AM
Russ
Russ - avatar
+ 1
Thank you Russ, you've been a big help, and I'll try and work on that flaw.
4th Mar 2020, 12:43 PM
Olivia
Olivia - avatar
+ 1
Not a problem. Let me know if you need any more help.
4th Mar 2020, 2:08 PM
Russ
Russ - avatar
11th May 2020, 1:52 PM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar