題目鏈接:浙大版《Python 程序設計》題目集
a = int(input()) # 輸入整數的格式
b = int(input())
print(a + b)
第1章-2 從鍵盤輸入三個數到a,b,c中,按公式值輸出 (30分)
a, b, c = input().split() # split默認空格分割,返回的是字符串
a = int(a) # 轉換為int
b = int(b)
c = int(c)
print(b * b - 4 * a * c)
每個數轉換為int比較麻煩,也可以這樣寫:
# 用map將分割后的字符串類型轉換為int類型
a, b, c = map(int, input().split())
print(b * b - 4 * a * c)
print("Python語言簡單易學".encode("utf-8"))
m = int(input())
s = 0
for i in range(11, m + 1): # 左閉右開
s = s + i
print("sum = %d" % s) # 格式化輸出
x = float(input()) # 輸入實數的格式,python沒有double類型!
if x == 0: # 不用打括號
print("f(0.0) = 0.0")
else:
print("f(%.1f) = %.1f" % (x, 1 / x))
# 可不用強制轉換為float,直接寫表達式,默認按實數計算
x = float(input())
if x < 0:
print("Invalid Value!")
else:
if x <= 50:
cost = x * 0.53
else:
cost = 50 * 0.53 + (x - 50) * 0.58
print("cost = %.2f" % cost)
a, n = map(int, input().split())
s = 0
x = 0
for i in range(n):
x = x * 10 + a
s += x
print("s = %d" % s)
n = int(input())
s = 0
for i in range(1, n + 1):
t = 2 * i - 1
s += 1 / t
print("sum = %.6f" % s)
n = int(input())
s = 0
f = 1
for i in range(1, n + 1):
x = 2 * i - 1
s += f * i / x
f = -f
print("%.3f" % s)
a, b = map(int, input().split(','))
ans = 0
for i in range(1, b + 1):
ans = ans * 10 + a
print(ans)
a, b = input().split(',')
ans = int(a, int(b)) # 字符串a表示的底數是b進制,返回轉換為10進制的數
print(ans)
s = list(map(int, input().split())) # 單行輸入不定個整數,儲存在列表中
s.sort() # 列表排序
print("%d->%d->%d" % (s[0], s[1], s[2]))
a, b = map(int, input().split())
if a > b:
print("Invalid.")
else:
print("fahr celsius")
for i in range(a, b + 1, 2): # [a,b],步長為2
print("%d%6.1f" % (i, 5 * (i - 32) / 9))
# 右對齊占6個字符:%6
# 左對齊占6個字符:%-6
a, b = map(int, input().split())
s = 0
for i in range(a, b + 1):
s += i * i + 1 / i
print("sum = %.6f" % s)
import math
a, b, c = map(int, input().split())
if a + b > c and a + c > b and b + c > a: # 用and而不是&&
p = (a + b + c) / 2
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
print("area = %.2f; perimeter = %.2f" % (area, 2 * p))
else:
print("These sides do not correspond to a valid triangle")
x = int(input())
if x <= 15:
y = 4 * x / 3
else:
y = 2.5 * x - 17.5
print("%.2f" % y)
a, b = map(int, input().split())
cnt = 0
s = 0
for i in range(a, b + 1):
cnt += 1 # 寫成cnt++報錯
s += i
if cnt % 5 == 0 or cnt == b - a + 1:
print("%5d" % i)
else:
print("%5d" % i, end="") # 不換行的格式end=""
print("Sum = %d" % s)