如果这是一个非常愚蠢的问题,我很抱歉;我是一名英语专业的学生,正在选修 COSC 课程(我知道这是愚蠢的决定),所以这一切对我来说都不容易。这是我要解决的问题:编写一个程序,接收一个包含消息的文件。消息可以是任意长度。您的程序将包含两个函数。第一个应该有一个处理文件内容的函数。第二个将获取第一个函数的输出,并将消息打印在一个星号框中。输入文件应该只是一个单行消息(参见附加的输入示例)。输出应采用该消息,将其拆分为两行,并将其置于框中(请参阅附加的输出示例)。这是我目前拥有的代码: def func1(process_contents): infile=open("october.txt", "r") process_contents=print(infile.read()) return process_contents def func2(): outfile=open("october_output.txt", "w") print(func1(message), file=outfile) outfile.close()(我还没有尝试创建星号框......我只是想让这些功能首先工作)。我知道我不能将另一个变量(即“消息”)分配给 func1 中的形式参数“process_contents”,我需要分配一个 VALUE 作为实际参数......但是因为我没有在这个问题中使用数字,我使用的价值是什么?
2 回答

喵喔喔
TA贡献1735条经验 获得超5个赞
您不一定需要在func1().
def func1():
infile = open("october.txt", "r")
process_contents = infile.read()
return process_contents
def func2():
message = func1()
print("asterisks" + message + "box thingy")
虽然,您可以使用形参来概括函数,如下所示:
FILENAME = "october.txt" # or rather something like sys.argv[1]
def func1(FILENAME):
infile = open(FILENAME, "r")
process_contents = infile.read()
return process_contents
def func2():
message = func1(FILENAME)
print("asterisks" + message + "box thingy")
- 2 回答
- 0 关注
- 159 浏览
添加回答
举报
0/150
提交
取消