+ 1
Simple Python question: how can I read 2 numbers from the same line?
How can I read 2 numbers from the same line that are separated by a space e.g: 1 2 not: 1 2 I tried this, but it does not work: a,b=int(input()),int(input()) print(a-b) Thank you for your time.
6 Respuestas
+ 3
Try this:
(a, b) = (int(k) for k in input().split())
0
portpass
It seems to be harder the I thought. I did not learn the thing you wrote there but I will try. Thank you!
0
Ok. All you need is split() method. You can rewrite the code in simpler way, but a bit longer:
(a, b) = input().split()
a = int(a)
b = int(b)
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2456/
0
portpass Thank you for your time, it is working, but how can I do the same thing with 4 numbers?
0
Stefan Secrieru,
You really can do the same with 4 numbers:
a, b, c, d = input().split()
a = int(a)
...
or you can use the fist variant:
a, b, c, d = (int(k) for k in input().split())
If you want to operate with more variables, try using lists and loops:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2431/
0
Oh... Thank you!