网络程序设计 实验二 1.无穷级数 2.单词统计 3.播放高音
public-time:2020-10-10 10:14
1.编写一个程序,求无穷级数的和sum=1+1/2+1/3+…1/n ,直到最后一项的精度小于0.000001。'''
def sum_1_n(n, accuracy):
sum = 0
for i in range(1, n + 1):
e = 1.0 / i
if e >= accuracy:
sum += e
else:
break
return sum
if __name__ == '__main__':
print('''求无穷级数的和sum=1+1/2+1/3+…1/n ''')
n = int(input("请输入n:"))
print("sum=1+1/2+1/3+…1/n={}".format(sum_1_n(n, 0.000001)))
import re
def sort(dic):
# return list[]
return sorted(dic.items(), key=lambda x: x[1], reverse=True)
def sum_1_n(s):
count_word = dict()
sum = re.split('[ .?]', s)
sum = [i.lower() for i in sum]
for i in sum:
if (i != '') and (i not in count_word):
count_word[i] = sum.count(i)
# return dict{}
return count_word
if __name__ == '__main__':
s = "The quick brown box jumped over the lazy dog. An apple a day keeps the doctor away. Can a fox and a dog be friends?"
print("单词出现的次数:")
word_count = sort(sum_1_n(s))
print("{:<8}\t{}".format("单词", "次数"))
for i in word_count:
print("{:<8}\t{}".format(i[0], i[1]))
import random
import winsound
def play_sound():
n = random.randint(1, 7)
winsound.Beep(n*100, 1000)
return n
def play():
answer = play_sound()
n = int(input("输入一个音高值(1-7):"))
if n == answer:
s = "你输入的正确"
else:
s = "你输入的不正确"
print("\t{}".format(s), end="\n\n")
play()
if __name__ == '__main__':
play()