Python for Syntax

Asked 2 years ago, Updated 2 years ago, 17 views

ans=input().split('-')

sum=0
for i in ans[0].split('+'):
    sum+=int(i)

for i in ans[1:]:
    for j in i.split('+'):
        sum-=int(j)

print(sum)

In this phrase

ans=input().split('-')

sum=0
for i in ans[0].split('+'):
    sum+=int(i)

for j in ans[1:].split('+'):     
        sum-=int(j)

print(sum)

Why

for j in ans[1:].split('+'):     
        sum-=int(j) 

for i in ans[1:]:
    for j in i.split('+'):
        sum-=int(j)

"Is this the right phrase to use? In the ans function, the number that removes the + from paragraph 1 I think I can use it right away, but I wonder why it is grammatically wrong

python

2022-09-20 10:49

1 Answers

An[1:] is an array. But the split function cannot be written to an array, it can only be written to a string. So you have to write for i in an [1:]: for jin i.split('+'): ...


2022-09-20 10:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.