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

why does my code multiply 2 times instead of one

My code should verify if a number is even, if it is, it prints the number multiplied by 2, if it isn’t, it should print the number multiplied by 3, but just doesn’t work.

m = int(input())
for i in range(m):
    n = int(input())
    n*=2 if n%2==0 else n*3
    print(n)

When i try this input:

3
1
2
3

It returns:

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

3
4
**27** <- ?

>Solution :

n *= 2 if n % 2 == 0 else n * 3

means

n *= (2 if n % 2 == 0 else n * 3)

which means

if n % 2 == 0:
    n = n * 2
else:
    n = n * n * 3

You meant to write

n *= 2 if n % 2 == 0 else 3
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