简体   繁体   English

Python 返回函数返回 None 而不是 True

[英]Python Return Function Returning None instead of True

I am trying to complete a task for my computer science class and I have a function that should be returning True but is returning None.我正在尝试为我的计算机科学课完成一项任务,我有一个应该返回 True 但返回 None 的函数。 I have not finished the code but this section should be working properly.我还没有完成代码,但这部分应该可以正常工作。

The code does look in the if statement as other prints that I have included have worked.该代码确实在 if 语句中查找,因为我包含的其他打印件已经工作。 I have also tried to make the code return a variable and define the varable in the if statements, which also does not work.我还尝试使代码返回一个变量并在 if 语句中定义该变量,这也不起作用。

RANK_ORDER = '34567890JQKA2'
SUIT_ORDER = 'DCHS'
def is_higher(card1, card2):
    card1v = RANK_ORDER.index(card1[0][0])
    card2v = RANK_ORDER.index(card2[0][0])
    card1s = SUIT_ORDER.index(card1[1][:1])
    card2s = SUIT_ORDER.index(card2[1][:1])
    if card1v == card2v:
        if card1s > card2s:
            return True
        else: 
            return False
    elif card1v > card2v:
        return True
    else:
        return False

def sort_cards(cards):
    is_higher(cards[0], cards[1])

if __name__ == '__main__':
    print(sort_cards(['AS', '5H']))

The code doesnt yet sort the cards, but is should say True if the first card is higher.该代码尚未对卡片进行排序,但如果第一张卡片较高,则应为 True。 This code prints None.此代码打印无。

is_higher returns a value to sort_cards , but you are printing sort_cards return, wich is None, you want to print the return of sort_cards that is the return of the return of is_higher . is_higher返回一个值给sort_cards ,但你要打印sort_cards回报,至极为None,要打印sort_cards的回报是回报的回报is_higher Just do this:只需这样做:

def sort_cards(cards):
    return is_higher(cards[0], cards[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM