Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I shorten this few lines of python code? [solved]

In python how can I shorten the code below?

if x == 15:
   y = 0
elif x == 14:
   y = 1
elif x == 13:
   y = 2
...
...
...
elif x == 0:
   y = 15

Currently the solutions I’ve gotten are

y = 15-x

and adding a dictionary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There Is A Pattern Your Code if x = 15 then y = 0 and if x = 14 then y = 1.
If you just subtract x from 15 then you get the value of y.
There is an equation made (if you know about math):

x+y = 15
then,
y = 15 -x

This Is The Code.

x = #value you use
y = 15 -x
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading