Python程序设计与算法基础教程(第二版)微课版第五章上机实践答案

Python程序设计与算法基础教程(第二版)微课版第五章上机实践答案

2.

str = input("请输入字符串:")
count=0
for i in str.split(' '):
        count += 1
print(count)

3.

a = [1,5,2,53,32,6,3,2,1,6]
b = []
for i in a :
    if i not in b :
        b.append(i)
print(b)    

4.

s=[9,7,8,3,2,1,55,6]

#内置函数
num = len(s) #元素个数
Max = max(s) #最大值
Min = min(s) #最小值
Sum = sum(s) #元素之和
Ave = sum(s) / len(s) #平均值

#直接访问列表元素
max = s[0]
min = s[0]
sum = 0
num = 0
for i in s:
    num += 1
    sum += i
    if(i > max) : max = i
    if(i < min) : min = i
ave = sum / num

#间接访问列表元素
max = s[0]
min = s[0]
sum = 0
num = 0
for i in range(0,len(s)) :
    if(s[i]>max) :max = s[i]
    if(s[i] < min) : min = s[i]
    num+=1
    sum+=s[i]
ave = sum / num

#正序访问
max = s[0]
min = s[0]
sum = 0
num = 0
i = 0
while i < len(S):
    if(s[i]>max) :max = s[i]
    if(s[i] < min) : min = s[i]
    i +=1
    sum+=s[i]
ave = sum / i

#反序访问
max = s[0]
min = s[0]
sum = 0
num = len(s)
i = num-1
while(i >= 0) :
    sum += s[i]
    if(s[i]>max) :max = s[i]
    if(s[i] < min) : min = s[i]
    i -= 1
ave = sum / num

# While True: .....break方法
max = s[0]
min = s[0]
sum = 0
num = len(s)
i = 0
while(True) :
    if(i > num-1) : break
    sum += s[i]
    if(s[i]>max) :max = s[i]
    if(s[i] < min) : min = s[i]
    i += 1

5.

s = [9,7,8,3,2,1,5,6]
for i in range(0, len(s)) :
    if (s[i] % 2 == 0) :
        s[i] = s[i] ** 2
print(s)

6.

s = input("请输入一个字符串:")
l = list()
for i in s :
    l.append(ord(i))
print(l)

满怀希望就会所向披靡

你可能感兴趣的:(Python)