elif Statements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

elif Statements

Can someone point out what is wrong with this code? number = int(input()) if number % 2 == 0: print(number * 2) elif number % 3 == 1: print(number * 3) elif number == 0: print(0) Here is the problem: Write a program that takes a number as input and - returns its double, if the number is even - returns its triple, if the number is odd - returns 0, if number is 0 Sample Input: 1 Sample Output: 3 An integer is even if it is divisible by two and odd if it is not even.

3rd Oct 2021, 7:22 PM
Aldrin S
Aldrin S - avatar
5 Answers
+ 5
Kamil Hamid , even if your code is running correctly, i would not recommend to do it like this. if there is a case how to handle a 0 from input, it has to be obvious. your code may look cool, tricky and whatever, but it lacks in readability and can create problems in case of maintaining. the zen of python says: ▪︎Explicit is better than implicit. ▪︎Readability counts. happy coding!
3rd Oct 2021, 8:10 PM
Lothar
Lothar - avatar
+ 4
In addition to what Terel Scmitt said, can I point out another (unnecessary) thing. Since 0%2 and 0 are equal, and 0*2 is 0, the last statement is not needed. As well as this, since there are only odd and even numbers, the second statement could be just an else statement like so: number = int(input()) if number % 2 == 0: print(number * 2) else: print(number * 3)
3rd Oct 2021, 7:30 PM
Kamil Hamid
Kamil Hamid - avatar
+ 3
The number after 1 will have to be 7, 13, 19... to satisfy the second condition “number % 3 == 1”, change it to “number % 2 == 1” so it'll check whether the remainder of the number is 1.
3rd Oct 2021, 7:26 PM
Tim
Tim - avatar
+ 3
I would add to what Lothar said that when speed and efficiency are important python isn't the best choice in the first place. So readability is often preferable to efficiency. If you want to go the efficiency route anyway you should make ample use of comments.
3rd Oct 2021, 8:23 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Thanks you all for the help. Also learned something with all the response.
3rd Oct 2021, 11:28 PM
Aldrin S
Aldrin S - avatar