0

# Stuck at elif Statements V

Here's what needs to be done: elif Statements 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 Here's what I have done: number = int(input()) #your code goes here if number // 2: print(number + number) elif not number // 2: print(number + number + number) elif num == 0: print(num) But the 4th one is still failing. Can someone please help me. Thank you!

27th Jan 2021, 2:15 PM
Andreas
4 ответов
+ 5
For example if number is even like 4 then it will return 2 and if number is odd like 5 , then it will return 2.5 , therefore "if number//2" will be true for both odd and even ,so it never goes to next elif statements unless the number is 0. Instead for even number check it as : If number%2==0: And for odd as: if number%2!=0:
27th Jan 2021, 2:25 PM
Abhay
+ 3
You don't want to use; number // 2 This will end up being true in most cases, even when odd, as it will return the quotient from division of 2 as a result. You want to use the modulo operator % and get the remainder of division instead. if number == 0: return 0 elif number % 2 == 0: #even return number * 2 else: return number * 3
27th Jan 2021, 2:27 PM
ChaoticDawg
0
Thanks, below the solution: number = int(input()) #your code goes here if number%2==0: print(number + number) elif number%2!=0: print(number + number + number) elif num == 0: print(num)
27th Jan 2021, 3:08 PM
Andreas
0
num in the last branch is undefined. This gives no error because python is interpreted and that code is unreachable (0%2=0). So you can simplify: if number%2==0: print(number*2) else: print(number*3)
27th Jan 2021, 4:16 PM
Benjamin Jürgens
Актуальное сегодня