I'm trying to calculate score for a problem, and I created this code as a sort of sandbox area for my main code. This function is supposed to calculate score as determined by what space the person lands on.
Example: they land on an add space, whatever number is after add in the list gets added to the score.
I decided to split the items in the list and then use a for loop and if statement to go through every item in the list and check what the operation was and then calculate the score, but I keep getting nothing but 0 as the score. The list separates fine but it doesn't calculate the score correctly.
game_map = ['nop', 'mul 65', 'sub 20', 'add 75', 'hlt']
pos = 0
map_split = [map.split() for map in game_map]
print(map_split)
score = 0
for j in range(0, len(map_split)):
if "mul" in map_split:
score *= int(map_split[j][1])
elif "sub" in map_split:
score -= int(map_split[j][1])
elif "add" in map_split:
score = int(map_split[j][1])
elif "nop" in map_split:
score = score
elif "jmp" in map_split:
score = score
print(score)
CodePudding user response:
for j in map_split:
if "mul" in j:
score *= int(j[1])
elif "sub" in j:
score -= int(j[1])
elif "add" in j:
score = int(j[1])
print(score)