binary - python addition 2 digit number -


i'm studying algorithms. exercise consist in put number of 2 digits (between 10 , 99) , addition of 2 digits. made in python , works, teacher said there's way without conversions i'm using. can me? there better way? thanks.

for in range(5):     add = 0     num = input("number: ")     num = int(num)     if num > 9 , num < 100:         num = str(num)         add = int(num[0]) + int(num[1])         print("the addition of 2 digits is: " + str(add))     else:         print("it not 2 digit number.") 

i think meant:

(num // 10) + (num % 10) 

with num // 10 perform integer division 10. first digit. num % 10 remainder of division, second digit. example:

>>> 67 // 10 6 >>> 67 % 10 7 

the succinct way must be:

sum(divmod(num, 10)) 

because divmod performs integer division 10 , finding remainder @ same time. sum sum of 2 numbers. example:

>>> divmod(67, 10) (6, 7) >>> sum(divmod(67, 10)) 13 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -