0

How to use python one-liners

How can I implement the with...as statement from python in an one-liner, like doing [print(i) for i in range(100)] for printing every number from 0 to 99, I can't find out anything for it Here is my try https://code.sololearn.com/claAHAPKpe32/?ref=app I tried to solve a problem where you are given a number x and k, and print out the sum of all the numbers which result by splitting x every k character(if the las number isn't of k length, zeros are added to satisfy this need) Here is a working code for this problem, to understand how it is done https://code.sololearn.com/cHCpNRzod0Dd/?ref=app

18th Dec 2018, 8:41 PM
Andrei Cereanu
Andrei Cereanu - avatar
4 Antworten
+ 2
Have you tried to implement the “with ... as” statement in your working code?
19th Dec 2018, 5:06 AM
Diego
Diego - avatar
+ 1
#Diego Acero yes I have, but I get some kind of syntax error in some places where it didnt pop up before :)) #try this: x = input().split() k=int(x[1]) #arr= if len(arr[-1])!=x[1]: (arr[-1]=str(int(arr[-1])*(10**(k-len(arr[-1])))) with arr as [x[0][i:i+k] for i in range(0, len(x[0]), k)]) print(eval('+'.join(arr)))
26th Dec 2018, 3:11 PM
Andrei Cereanu
Andrei Cereanu - avatar
+ 1
Andrei Cereanu Are you sure that the “with ... as” statement can be used like you intend to? Is this some kind of homework/assingment? Because there are much simpler ways to achieve what you want.
26th Dec 2018, 3:46 PM
Diego
Diego - avatar
0
Diego Acero no, this is a problem I got from a friend and I wanted to solve it in as few lines as possible, without cheesy tricks like using semicolons to separate lines of code and stuff I am not sure if it can be used as I intend it to but I wanted to try for myself and ask some more people about it sooo... sorry 😅
26th Dec 2018, 3:49 PM
Andrei Cereanu
Andrei Cereanu - avatar