Search code examples
additionpython-3.5input-split

Python Input Split with a limit range


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"


Solution

  • a,b = map(int, input().split())
    
    while True:
        if (0 <= a <= 9) and (0 <= b <= 9):
            c = a + b
            break
    
    print('%r' %c)