python3 100例 一码人学习笔记(71-80)

题目71:编写input()和output()函数输入,输出5个学生的数据记录。

N = 3
#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:\n')
        stu[i][1] =input('input student name:\n')
        for j in range(3):
            stu[i][2].append(int(input('score:\n')))
 
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)
nput student num:
1
input student name:
小红
score:
60
score:
40
score:
80
input student num:
2
input student name:
小明
score:
34
score:
34
score:
1
input student num:
3
input student name:
小王
score:
90
score:
89
score:
100
[['1', '小红', [60, 40, 80]], ['2', '小明', [34, 34, 1]], ['3', '小王', [90, 89, 100]], ['', '', []], ['', '', []]]
1     小红
60
40
80
2     小明
34
34
1
3     小王
90
89
100
Press any key to continue . . .

题目72:创建一个链表。

l=[ input("please input a number:\n") for i in range(5)]
print(l)
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']
Press any key to continue . . .

题目73:反向输出一个链表。

l=[ input("please input a number:\n") for i in range(5)]
o=l[::-1]
print(o)
please input a number:
12
please input a number:
13
please input a number:
141
please input a number:
15
please input a number:
16
['16', '15', '141', '13', '12']
Press any key to continue . . .

 题目74:列表排序及连接。

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]
Press any key to continue . . .

题目75:放松一下,算一道简单的题目。

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
Press any key to continue . . .

题目76:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

def sum(n):
    if n==1:
        return 1
    elif n==2:
        return 1/2
    elif n%2==0:
        return (1/n+sum(n-2))
    elif n%2==1:
        return (1/n+sum(n-2))
n=int(input("input a number:\n"))
print(sum(n))
input a number:
4
0.75
Press any key to continue . . .

题目77:循环输出列表

if __name__ == '__main__':
    s = ["man","woman","girl","boy","sister"]
    for i in range(len(s)):
        print(s[i])
man
woman
girl
boy
sister
Press any key to continue . . .

题目78:找到年龄最大的人,并输出。请找出程序中有什么问题。

person = {"li":18,"wang":50,"zhang":20,"sun":22}
def find_max(dict):
    max_age = 0
    for key, value in dict.items():
        if value > max_age:
            max_age = value
            name = key
    print(name)
    print(max_age)
find_max(person)
wang
50
Press any key to continue . . .

题目79:字符串排序。 

if __name__ == '__main__':
    str1 = input('input string:\n')
    str2 = input('input string:\n')
    str3 = input('input string:\n')
    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.')
    print(str1,str2,str3)
input string:
TY
input string:
U
input string:
I
TY U I
after being sorted.
I TY U
Press any key to continue . . .

题目80:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子? 

start,end,m1=0,100,0
while m1==0:
    end=end*2
    for i in range(start,end):
        m5=5*i+1
        if m5%4==0:
            m4=(m5/4)*5+1
            if m4%4==0:
                m3=(m4/4)*5+1
                if m3%4==0:
                    m2=(m3/4)*5+1
                    if m2%4==0:
                        m1=(m2/4)*5+1
                        break
    start=i
print("最少为:%d个桃子" % m1)
最少为:3121个桃子
Press any key to continue . . .

猜你喜欢

转载自blog.csdn.net/qq_41905045/article/details/81183427