簡體   English   中英

兩個 Python 代碼之間的詳細區別是什么,它們都是為了在三個輸入中找到最大數而編寫的

[英]What is the detailed difference between two Python codes, both written to find the largest number among three inputs

我如何理解以下兩個代碼之間的詳細區別,以找到三個數字中的最大數字。

代碼1:

def max_num(num1, num2, num3):
    if num1 >= num2 and num1 >= num3:
        return num1
    elif num2 >= num1 and num2 >= num3
        return num2
    else:
        return num3

代碼2:

def max_num(num1, num2, num3):
    if num1 > num2 and num1 > num3:
        return num1
    elif num2 > num1 and num2 > num3
        return num2
    else:
        return num3

編輯:如果 num1 == num2,即在 [6, 6, 5] 中,Code2 可能會錯誤地將 num3 返回為最大值。

如上所述,主要區別在於“大於”與“大於或等於”的使用。 我認為唯一重要的地方是當列表中有多個相等的值時,在這種情況下 Code2 將返回第一個實例,而 Code3 將返回最后一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM