Python:函数似乎没有返回任何值

时间:2018-03-13 07:50:03

标签: python function compare

这是比较功能:

String

当我运行它时,它会提示用户输入a和b但是在输入之后程序没有做任何事情它没有返回。 为什么会这样?

2 个答案:

答案 0 :(得分:0)

return关键字返回数据。如果要查看输出,请使用print。 尝试

print(compare(a, b))

比较函数将return数字print将其打印到控制台/ IDLE

答案 1 :(得分:0)

您需要打印声明。

def compare(a, b):
    if a > b:
        return 1

    elif a == b:
        return 0

    else:
        return -1

a = int(input('Enter first number here:'))
b = int(input('Enter second number here:'))

print(compare(a, b)) # use this line if using python 3
# print compare(a, b) # use this line if using python 2
相关问题