DAY 13
0
Software Development

## [Day 17] name? main? if __name__ == '__main__' ? 兩眼一抹黑啊...

``````def C(m,n):
if n==0 or m==n:
return 1
elif n==1:
return m
else:
return C(m-1,n)+C(m-1,n-1)
print(C(10,3)) #output:120
print(C(25,10)) #output:3268760
``````

``````def difage(m,n):
#計算兩人的年紀差
diff=abs(m-n)
return diff

def sumage(m,n):
#計算兩人的年紀和
tot=abs(m+n)

#print(difage(62,30))
if __name__  == '__main__':
print(difage(62,30))
``````

# if name == 'main'有什麼作用嗎？

``````def difage(m,n):
diff=abs(m-n)
return diff

print('This __name__ is '+__name__)

if __name__  == '__main__':
print(difage(62,30))
``````

``````>>> python age_1.py
This __name__ is __main__
32
``````

``````def difage(m,n):
diff=abs(m-n)
return diff

print('This __name__ is '+__name__)
print(difage(62,30))
``````

# 但真的是這樣嗎？

``````# main_1.py
from age_1 import difage
print('In main, the __name__ is '+__name__)
print(difage(50,30))
``````
``````# main_2.py
from age_2 import difage
print('In main, the __name__ is '+__name__)
print(difage(50,30))
``````

``````>>> python main_1.py
This __name__ is age_1
In main, the __name__ is __main__
20
``````
``````>>> python main_2.py
This __name__ is age_2
32
In main, the __name__ is __main__
20
``````