1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken 10 3
sum = 0f = 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()
原文地址:https://www.cnblogs.com/chenjiao0904/p/9983810.html
时间: 2024-10-18 12:50:41