I'd like to include it.

Asked 2 years ago, Updated 2 years ago, 36 views

Regarding this for statement, what should I do to include it?

x,n=map(int,input().split())
num = [int(input()) for_in range(n)]

num1 = 0
for in num:
    num1+=i
    z = x-num1 
    print(z)

python python3

2022-09-30 15:47

2 Answers

With Python 3.8 and later, you can use := (walrus operator/ walrus operator) to write:

 num1 = 0
_ = [print(x-(num1:=num1+i)) for in num]


2022-09-30 15:47

An implementation using itertools.accumulate() is likely.Please adjust the print output separately, as the calculation results can be obtained as a list.

import it tools

x,n = map(int, input().split())
num = [int(input()) for_in range(n)]

z = [x-n for n intertools.accumulate(num)]
print(z)


2022-09-30 15:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.