0

So I'm still struggling to understand what actually happens when the parentheses of a function that takes in an argument is missing. In this case, I noticed that when the convention.upper brackets are missing, the 3rd block of the if statement code (ie input proper convention) is runned and then the kernel dies although the intended output is correct and works. Can anyone explain to me what happens if the brackets are missing?

temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]

if i_convention.upper() == "C":
  result = int(round((9 * degree) / 5 + 32))
  o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
  result = int(round((degree - 32) * 5 / 9))
  o_convention = "Celsius"
else:
  print("Input proper convention.")
  quit()
print("The temperature in", o_convention, "is", result, "degrees.")


VLAZ
  • 26,331
  • 9
  • 49
  • 67
clover
  • 301
  • 1
  • 3
  • 13

1 Answers1

0

That means you're not calling the function, but are just looking at the function object itself. i_convention.upper == "C" compares the upper function object with the string "C", which will never be True, because a function isn't equal to a string.

A visualisation of what you're working with:

>>> print(str.upper)
<method 'upper' of 'str' objects>

Functions are just like any other value in Python; they can be passed around and compared too, without being called.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks deceze, I understand that without the brackets, I'm only calling the method not the value of the method, is that right? Any idea why would this still yield an output since the first 2 if arguments does not give a 'True' ?? – clover Dec 09 '19 at 07:57
  • You're *not* ***calling*** the method. Calling is specifically to *run/execute/CALL* a function using `()`. Without `()`, you're *not* calling the function and you're just using the function object itself as a value. If you call the function, you're receiving its *return value* and are using it instead. And your program will still yield *some* output, but that `if` condition simply isn't `True`. – deceze Dec 09 '19 at 07:59