Python教材课后编程习题

第一章

Python教材课后编程习题_第1张图片

第一题

a = int(input())
b = int(input())
print(a+b)

第二题

a = int(input())
b = int(input())
c = int(input())
print(b*b-4*a*c)

第三题

print("Python语言简单易学")

第二章Python教材课后编程习题_第2张图片

第一题

sum = 0
n = int(input())
for i in range(1, n+1):
    sum += i
print(sum)

第二题

x = float(input())
if x == 0:
    print("f(%.1f)=0.0" % x)
else:
    print("f(%.1f)=%.1f" % (x, (1/x)))

第三题

cost = float(input())
if cost < 0:
    print("Invalid Value!")
elif cost <= 50:
    print("cost=%d" % (cost*0.53))
else:
    print("cost=%d" % (50*0.53 + (cost-50)*(0.53+0.05)))

第四题

a = input()
n = int(input())
print("s = %d" % (sum([int(a*i) for i in range(1, n+1)])))

第五题

sum = 0
a = input()
for i in a.split(' '):
    if int(i) <= 0:
        break
    if int(i) % 2 != 0:
        sum += int(i)
print(sum)

第六题

n = int(input())
sum = 0
flag = 1
m = 1
for i in range(1, n+1):
    if flag == 1:
        sum += i / m
    else:
        sum -= i / m
    m += 2
    flag *= -1
print("%.3f" % sum)

第三章Python教材课后编程习题_第3张图片

第一题

a = input()
s = sum(int(i) for i in a.split())
s /= len(a.split())
for i in a.split(' '):
    if int(i) > s:
        print("%d " % int(i), end='')

第二题

a = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
m = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
q = input()
sum = 0
for i in range(0, len(q)-1):
    sum += a[int(q[i])]
z = m[sum%11]
if z == q[len(q)-1]:
    print('yes')
else:
    print('no')

第四章

 Python教材课后编程习题_第4张图片

第一题

for i in range(1,6):
     print('*'*i)

for i in range(1,6):
     print(' '*(5-i),end='')
     print('*'*(2*i-1))

for i in range(1,4):
    print(' '*(3-i),end='')
    print('*'*(2*i-1))
for i in range(2,0,-1):
    print(' ' * (3-i), end='')
    print('*' * (2 * i - 1))

第二题

a = list(range(1,31))
k = 1
flag = 0
x = 0
while flag != 10:
    if x >= len(a):
        x = x - len(a)
    if k == 9:
        flag += 1
        print(a.pop(x),end=' ')
        k = 1
    k += 1
    x += 1

第三题

def a(i):
    sum = 0
    for j in range(1,i):
        if i % j ==0:
            sum += j
    if sum == i:
        return True
    else:
        return False
for i in range(6,1000):
    if a(i):
        print(i,end=' ')

第四题

import math
def s(a):
    i = 2
    while i <= math.sqrt(a):
        if a % i ==0:
            return 0
        i += 1
    return a
sum1 = 0
for i in range(101,1000,2):
    sum1 += s(i)
print(sum1)

第五题

k = 1
for i in range(9):
    k += 1
    k *= 2
print(k)

第六题

a = float(input())
c = input()
b = float(input())
if c == '+':
    print("%.2f" % (a+b))
if c == '-':
    print("%.2f" % (a-b))
if c == '*':
    print("%.2f" % (a*b))
if c == '/':
    print("%.2f" % (a/b))

第七题

a, b = input().split(' ')
a = int(a)
k = 0
for i in range(a):
    x = input()
    k += sum([int(j) for j in list(x.split(' '))])
print(k)

第五章

Python教材课后编程习题_第5张图片

第一题

题目有误

第二题

a = {'6': 0, '7': 0, '8': 0, '9': 0}
x = input().split(',')
for i in x:
    if i >= '6' and i <= '9':
        a[i] += 1
for i in a:
    if a[i] == 0:
        print(i)

第三题

a = input().split()
m = len(a)
a = set(a)
if m != len(a):
    print('True')
else:
    print('Flase')

你可能感兴趣的:(python,python,开发语言,后端)