Python3.x 基础练习题100例(71-80)
2021-06-09 18:05
标签:inpu 结果 turn continue 列表排序 reverse int lis 练习 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: 题目: 程序: 输出结果: Python3.x 基础练习题100例(71-80) 标签:inpu 结果 turn continue 列表排序 reverse int lis 练习 原文地址:https://www.cnblogs.com/youcans/p/14485477.html练习71:
编写input()和output()函数输入,输出5个学生的数据记录。N = 5
# stu
# num : string
# name : string
# score[4]: list
student = []
for i in range(5):
student.append([‘‘, ‘‘, []])
def input_stu(stu):
for i in range(N):
stu[i][0] = input(‘input student num:‘)
stu[i][1] = input(‘input student name:‘)
for j in range(3):
stu[i][2].append(int(input(‘score:‘)))
def output_stu(stu):
for i in range(N):
print(‘%-6s%-10s‘ % (stu[i][0], stu[i][1]))
for j in range(3):
print(‘%-8d‘ % stu[i][2][j])
if __name__ == ‘__main__‘:
input_stu(student)
print(student)
output_stu(student)
input student num:1
input student name:d
score:3
score:4
score:65
input student num:2
input student name:e
score:3
score:54
score:7
input student num:3
input student name:fds
score:3
score:5
score:6
input student num:4
input student name:s
score:3
score:45
score:67
input student num:5
input student name:rs
score:3
score:4
score:6
[[‘, ‘rs‘, [3, 4, 6]]]
1 d
3
4
65
2 e
3
54
7
3 fds
3
5
6
4 s
3
45
67
5 rs
3
4
6
练习72:
创建一个链表。if __name__ == ‘__main__‘:
ptr = []
for i in range(5):
num = int(input(‘please input a number:‘))
ptr.append(num)
print(ptr)
please input a number:4
please input a number:1
please input a number:2
please input a number:3
please input a number:6
[4, 1, 2, 3, 6]
练习73:
反向输出一个链表。if __name__ == ‘__main__‘:
ptr = []
for i in range(5):
num = int(input(‘please input a number:‘))
ptr.append(num)
print(ptr)
ptr.reverse()
print(ptr)
please input a number:1
please input a number:2
please input a number:3
please input a number:4
please input a number:5
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
练习74:
列表排序及连接。
分析:排序可使用 sort() 方法,连接可以使用 + 号或 extend() 方法。
程序:if __name__ == ‘__main__‘:
a = [1, 3, 2]
b = [3, 4, 5]
a.sort() # 对列表 a 进行排序
print(a)
# 连接列表 a 与 b
print(a + b)
# 连接列表 a 与 b
a.extend(b)
print(a)
[1, 2, 3]
[1, 2, 3, 3, 4, 5]
[1, 2, 3, 3, 4, 5]
练习75:
放松一下,算一道简单的题目。if __name__ == ‘__main__‘:
for i in range(5):
n = 0
if i != 1: n += 1
if i == 3: n += 1
if i == 4: n += 1
if i != 4: n += 1
if n == 3: print(64 + i)
67
练习76:
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/ndef peven(n):
i = 0
s = 0.0
for i in range(2, n + 1, 2):
s += 1.0 / i # Python里,整数除整数,只能得出整数,所以需要使用 浮点数 1.0
return s
def podd(n):
s = 0.0
for i in range(1, n + 1, 2):
s += 1.0 / i # Python里,整数除整数,只能得出整数,所以需要使用 浮点数 1.0
return s
def dcall(fp, n):
s = fp(n)
return s
if __name__ == ‘__main__‘:
n = int(input(‘input a number:\n‘))
if n % 2 == 0:
sum = dcall(peven, n)
else:
sum = dcall(podd, n)
print(sum)
input a number:
12
1.2249999999999999
练习77:
循环输出列表if __name__ == ‘__main__‘:
s = ["man", "woman", "girl", "boy", "sister"]
for i in range(len(s)):
print(s[i])
man
woman
girl
boy
sister
练习78:
找到年龄最大的人,并输出。请找出程序中有什么问题。if __name__ == ‘__main__‘:
person = {"li": 18, "wang": 50, "zhang": 80, "sun": 22}
m = ‘li‘
for key in person.keys():
if key != m:
if person[m]
zhang,80
练习79:
字符串排序。if __name__ == ‘__main__‘:
str1 = input(‘input string:‘)
str2 = input(‘input string:‘)
str3 = input(‘input string:‘)
print(str1, str2, str3)
if str1 > str2: str1, str2 = str2, str1
if str1 > str3: str1, str3 = str3, str1
if str2 > str3: str2, str3 = str3, str2
print(‘after being sorted:‘,end=" ")
print(str1, str2, str3)
input string:fasd
input string:fdse
input string:gbx
fasd fdse gbx
after being sorted: fasd fdse gbx
练习80:
海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二
只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原
来最少有多少个桃子?if __name__ == ‘__main__‘:
i = 0
j = 1
x = 0
while (i
3121.0