計算某個數的階乘
number = 5 # 計算3的階乘
re = 1 # 儲存最後算出來的結果,因為最初的數是1,1*任何數都是1
i = 1 # 臨時儲存當前階乘計算到哪個數了
while i <= number:
re *= i
i += 1
print(re)
列印1!-2!+3!-4!...+50!的結果
sum = 0
number = 1
while number <= 50:
result = 1 # 儲存最後計算的結果
i = 1
while i <= number:
result *= i
i += 1
# print(result)
if number % 2 ==0:
sum -= result
else:
sum += result
number += 1
print(sum)