1
var1,var2 = input("Enter two digits a and b (0-9):").split(' ')
while True:
    if (0 <= var1 <= 9) and (0 <= var2 <= 9):
        result = var1+var2
    print("The result is: %r." %result)

I use Spyder Python 3.5 to write this code and try to run it. However, this code does not work.

It reveals that " (1) var1,var2 = input("Enter two digits a and b (0-9):").split(''); (2) TypeError: 'str' object is not callable"

  • 1
    You must have named a previous variable `input` or `split`, overwriting the built-in functions. Don't do that. Additionally, your code will not work as expected, because `input()` returns strings, not ints. You'll need to call `var1 = int(var1)` and `var2 = int(var2)` to get integers you can then compare in your `if` statement. Finally, by putting the `if` statement and `print()` call under a `while True` loop, your program will continuously print the answer until you break out of it with Ctrl-C. I doubt that's what you want. – MattDMo Oct 10 '16 at 00:13
  • Thanks for your suggestion. I feel confused about 'overwrite the built-in function' and it still reveals that TypeError: 'str' object is not callable –  Oct 10 '16 at 00:18
  • 1
    Python has certain built-in functions available without importing anything, including `input`, `print`, `max`, `min`, etc., as well as the methods associated with the default data types - lists, dicts, ints, floats, strings, etc. `split()` is a string method, for example - you call it on a string. At some point in your session or code you must have assigned to a variable called `input`, and overwritten or masked the built-in function. – MattDMo Oct 10 '16 at 00:22

1 Answers1

0
a,b = map(int, input().split())

while True:
    if (0 <= a <= 9) and (0 <= b <= 9):
        c = a + b
        break

print('%r' %c)
Jeremy_Tamu
  • 725
  • 1
  • 8
  • 21