【问题标题】:greatest in number a,b,ca,b,c 中最大的
【发布时间】:2020-01-17 21:38:43
【问题描述】:
def greater(a,b):
    if a > b:
        return a
    elif a==b:
        return "equal"
    else:
        return b

def greatest(a,b,c):

    if a>b and a>c:
        return 'a'

    elif a==b and a==c:
        return "equal"

    elif b > a and b > c:
        return b

    elif b==a and b==c:
        return "equal"

    elif c==a and c==b:
        return "equal"

    else:
        return c

bigger = greater(1,1)        
biggest = greatest(bigger,1,1)
print(biggest)


error:Traceback (most recent call last):
  File "functions_execrise2.py", line 30, in <module>
    biggest = greatest(bigger,1,1)
  File "functions_execrise2.py", line 11, in greatest
    if a>=b and a>=c:
TypeError: '>=' not supported between instances of 'str' and 'int'

【问题讨论】:

  • 您正在尝试将字符串 "equal"1 进行比较
  • 您可以返回ab 而不是"equal",因为它们具有相同的值。

标签: python-3.x python-3.6 python-3.7


【解决方案1】:

毫无疑问,但是greater(1,1) 可能返回“equal”,因此您调用了greater("equal",1,1),导致您将字符串与整数进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2011-08-01
    • 2015-06-12
    • 2012-01-27
    • 2011-05-30
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多