0

if在函数中应用了一条语句,但它返回了冲突的结果。

功能是将单词与杂志进行匹配。if它在语句后返回“是”和“否” 。谁能看看我做错了什么?

测试输入是:

magazine=['give', 'me', 'one', 'grand', 'today', 'night']
note=['give', 'one', 'grand', 'today']
import math
import os
import random
import re
import sys
from collections import Counter

# Complete the checkMagazine function below.
def checkMagazine(magazine, note):
    m=Counter(magazine)
    d=Counter(note)
    match=False
    newdict = {k: m[k] for k in d}
    if newdict.items()>=d.items():
        match=True
    if match:
        print("Yes")
    print("No")

没有错误,只是与答案不符。答案应该是“是”,但我的函数同时返回“是”和“否”。

4

1 回答 1

0

你应该放在print('No')一个条件下。

def checkMagazine(magazine, note):
    m=Counter(magazine)
    d=Counter(note)
    match=False
    newdict = {k: m[k] for k in d}
    if newdict.items()>=d.items():
        match=True
    if match:
        print("Yes")
    if match==False:
        print("No")
于 2019-08-04T23:32:36.480 回答