python - 文件处理中的 int 函数和 str() 函数。我不知道哪里出了问题

标签 python file-handling

此任务所需的两个文件的路径已存储在变量 file_path_1 和 file_path_2 中

对file_path_1和file_path_2调用上一个任务中编写的函数readfile(),并将它们的消息语句分别存储在变量message_1和message_2中。

打印 message_1 和 message_2 以查看它们包含的内容。

编写一个函数fuse_msg():

将message_a和message_b作为参数

实现 message_a 除以 message_b 的整数(下限)除法(在应用下限除法之前不要忘记将消息设置为 int)并将商存储在名为 quotient 的变量中

以字符串格式返回商。

[Note you can convert any variable 'a' to string using str(a)] read_file() is already defined in another

message_1 = read_file(file_path_1)
message_2 = read_file(file_path_2)
print(message_1)
print(message_2)

def fuse_msg(message_a , message_b) :
    quotient =  int(message_b // message_a)
    return str(quotient)

secret_msg_1 = fuse_msg(message_1,message_2)

最佳答案

除法之前需要将message_b和message_a转换为int或float

message_1 = read_file(file_path_1)
message_2 = read_file(file_path_2)
print(message_1)
print(message_2)

def fuse_msg(message_a , message_b) :
    quotient =  int(message_b) // int(message_a)
    return str(quotient)

secret_msg_1 = fuse_msg(message_1,message_2)

关于python - 文件处理中的 int 函数和 str() 函数。我不知道哪里出了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477253/

相关文章:

尽管安装了 PIP,Python Pillow(或 PIL)仍无法正常工作

具有混合数据类型(文本、数字、分类)的 Python scikit-learn 分类

c++ - 当其标识符已经是结构的成员时如何使用枚举器列表项?

python - Flask-sqlalchemy:何时关闭 session ?

python - Qt 相当于 Python 的 range() 函数,返回 QList<int> 吗?

python - 3和5的倍数之和

python - 什么情况下python的f.readlines方法会失败?

c - 从文件中读取值并将其存储在二维矩阵中

Php创建一个文件,如果不存在

python - 有没有办法检查我的代码的哪一部分使文件句柄处于打开状态