【Python for Everybody(Python Data Structures)】Week 3 | Chapter 7 题目汇总

PY4E课程官网:https://www.py4e.com/

参考文章:题目及答案汇总(Github)












Assignment 7.1

Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below.You can download the sample data at http://www.pythonlearn.com/code/words.txt

编写一个程序,提示输入文件名,然后打开该文件并读取该文件,并打印大写的文件内容。使用文件word.txt来产生下面的输出。你可以在 http://www.pythonlearn.com/code/words.txt 下载样本数据。

import sys
fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print(fname, 'File does not exist ')
    sys.exit()
inp = fhand.read()
print(inp.upper())

>>>Enter the file name:test.txt
test.txt File does not exist 
An exception has occurred, use %tb to see the full traceback.

>>>Enter the file name:  words.txt
WRITING PROGRAMS OR PROGRAMMING IS A VERY CREATIVE
AND REWARDING ACTIVITY  YOU CAN WRITE PROGRAMS FOR
MANY REASONS RANGING FROM MAKING YOUR LIVING TO SOLVING
A DIFFICULT DATA ANALYSIS PROBLEM TO HAVING FUN TO HELPING
SOMEONE ELSE SOLVE A PROBLEM  THIS BOOK ASSUMES THAT
{\EM EVERYONE} NEEDS TO KNOW HOW TO PROGRAM AND THAT ONCE
YOU KNOW HOW TO PROGRAM, YOU WILL FIGURE OUT WHAT YOU WANT
TO DO WITH YOUR NEWFOUND SKILLS

WE ARE SURROUNDED IN OUR DAILY LIVES WITH COMPUTERS RANGING
FROM LAPTOPS TO CELL PHONES  WE CAN THINK OF THESE COMPUTERS
AS OUR PERSONAL ASSISTANTS WHO CAN TAKE CARE OF MANY THINGS
ON OUR BEHALF  THE HARDWARE IN OUR CURRENT-DAY COMPUTERS
IS ESSENTIALLY BUILT TO CONTINUOUSLY ASK US THE QUESTION
WHAT WOULD YOU LIKE ME TO DO NEXT

OUR COMPUTERS ARE FAST AND HAVE VASTS AMOUNTS OF MEMORY AND 
COULD BE VERY HELPFUL TO US IF WE ONLY KNEW THE LANGUAGE TO 
SPEAK TO EXPLAIN TO THE COMPUTER WHAT WE WOULD LIKE IT TO 
DO NEXT IF WE KNEW THIS LANGUAGE WE COULD TELL THE 
COMPUTER TO DO TASKS ON OUR BEHALF THAT WERE REPTITIVE  
INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST
ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING
AND MIND-NUMBING

Assignment 7.2

Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:  
X-DSPAM-Confidence:    0.8475  
Count these lines and extract the floating point values from each of the lines and compute the average
of those values and produce an output as shown below.
You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt when 
you are testing below enter mbox-short.txt as the file name.

编写一个程序,提示输入文件名,然后打开该文件并读取文件,寻找表格中的行。
X-DSPAM-Confidence:    0.8475
对这些行进行计数,并从每一行中提取浮点值,计算这些值的平均值,并产生如下图所示的输出。你可以在http://www.pythonlearn.com/code/mbox-short.txt 下载样本数据,当你在下面测试时输入mbox-short.txt作为文件名。

import sys
fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print(fname, 'File does not exist ')
    sys.exit()

count = sum = ave= 0
for line in fhand:
    if line.startswith("X-DSPAM-Confidence:"):
        count = count + 1
        colon_index = line.find(':')
        # make the string only remain whitespaces and the number
        value_str = line[colon_index + 1:]
        # strip whitespaces and type transition
        value = float(value_str.strip())
        sum += value
ave = sum/count
print('Count is:', count, "  Sum is:%.3f" % sum, "  Average is:%.3f" % ave)

>>>Enter the file name:  test.txt
test.txt File does not exist 
An exception has occurred, use %tb to see the full traceback.

>>>Enter the file name:  mbox-short.txt
Count is: 27   Sum is:20.269   Average is:0.751

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值