标签:readline app 运算 type ping utf-8 lin pen 内容
1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3
sum = 0
f = open("shoping.txt","r",encoding="utf-8")
for line in f.readlines():
#每读一行变成列表
#print(line)
line = line.strip().split(" ")
print(line)
a = line[1]
print(type(a))
sum =sum +int(line[1])*int(line[2])
#注意取出来的是字符串格式,做运算用int 强制转换下
print(sum)
f.close()
文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
标签:readline app 运算 type ping utf-8 lin pen 内容
原文地址:https://www.cnblogs.com/chenjiao0904/p/9983810.html