从零开始学Python【浙大版《Python 程序设计》题目集】
题目链接:浙大版《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)