返回列表 發帖

計算總和 (一)

計算 1+2+3+…+10 之和

  1. sum=0
  2. for i in range(1,11):
  3.     sum+=i
  4. print("1+2+3+...+10=%d"%sum)
複製代碼
  1. sum=0
  2. for i in range(1,11):
  3.     sum+=i   #sum=sum+i
  4. print("1+2+3+...+10={}".format(sum))
複製代碼

  1. sum=0
  2. for i in range(1,11):
  3.     sum=sum+i
  4. print(f"1+2+3+...+10={sum}")
複製代碼

TOP

  1. sum=0
  2. for a in range(1,11):
  3.     sum=sum+a
  4. print(f"sum={sum}")
複製代碼

TOP

  1. sum=0
  2. for i in range(1,11):
  3.     sum=sum+i
  4. print("sum=",sum)
複製代碼

TOP

  1. sum=0
  2. for i in range (1,11):
  3.     sum=sum+i
  4.     print(f"sum={sum}")
複製代碼

TOP

  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor

  4. This is a temporary script file.
  5. """
  6. sum=0   #存加總

  7. for i in range(1,11):
  8.     sum=sum+i
  9. print("sum=",sum)
複製代碼

TOP

  1. sum=0   #存加總

  2. for i in range(1,11):
  3.     sum=sum+i
  4. print("sum=",sum)
複製代碼

TOP

返回列表