'write each result of the loop to a python list
I have the txt file as follows:
0 1 2
0 // 7697 909
1 // 3536 4921
2 // 1421 6919
3 * 9805 -8620
4 + -862 -5869
... ... ... ...
2995 * 4329 9800
2996 + -6614 6404
2997 ** 15 94
2998 + -3110 -5
2999 // 3116 1619
where the first column number '0' is the action, the second is the number for the action. You need to go through all the lines and follow these steps. At the end write a dictionary sequentially the results for each tape. I can't write a dictionary with all the results. thank you
for line in calc_read.splitlines():
line = line.split(' ')
result_all=[]
if line[0] == '+':
result_sum_ = [int(item) for item in line[1:]]
total_sum_ = sum(result_sum_)
print(total_sum_)
if line[0] == '-':
result_minus_ = [int(item) for item in line[1:]]
#print(result_minus_)
total_minus_ = result_minus_[0] - result_minus_[1]
print(total_minus_)
if line[0] == '*':
result_umn_ = [int(item) for item in line[1:]]
#print(result_umn_)
total_umn_ = result_umn_[0] * result_umn_[1]
print(total_umn_)
if line[0] == '//':
result_del_ = [int(item) for item in line[1:]]
#print(result_del_)
total_del_ = result_del_[0] // result_del_[1]
print(total_del_)
if line[0] == '%':
result_ost_ = [int(item) for item in line[1:]]
#print(result_ost_)
total_ost_ = result_ost_[0] % result_ost_[1]
print(total_ost_)
if line[0] == '**':
result_sqr_ = [int(item) for item in line[1:]]
#print(result_sqr_)
total_sqr_ = result_sqr_[0] ** result_sqr_[1]
print(total_sqr_)
result_all.append([total_sum_, total_minus_, total_umn_, total_del_, total_ost_, total_sqr_])
print(result_all)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|