+ 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.

5 ответов

+ 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!

+ 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)

+ 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.

+ 2

Thanks you all for the help. Also learned something with all the response.