I keep getting an error whenever I enter this code (I’m a python noob so I’m probably missing something obvious)
def expadd(num, exp):
while ((num and exp) != (1001)):
return (num ^ exp) + expadd((num + 1), (exp + 1))
return 0
buffer = str(expadd(1000, 1000)
total = 0 #error here "syntax error"
for i in range(1,10):
total = total + int(buffer[-i])
print total
Besides the syntax error, there is also a logical error:
will always be
True, because both0and1are different from1001.(num and exp)checks if bothnumandexpareTrueish (which, for numbers, is the case if they are not0). The result of this will either be1(True) or0(False), and both of them are different from1001.Then, @interjay noted correctly that you should be using
if, notwhile.You probably meant
(the parentheses are not necessary, I just added them for clarity)
Also,
^is binaryxor, not exponentiation. That is the**operator: