'Conditional statement without using if-else in python

So here is the question. There are 2 variables having values of 3 and 5 respectively. if the user enters 3, "5" should be printed and vice versa. implement the code without using if-else. Any help would be really appreciated.



Solution 1:[1]

Use this:

# take integer input from user
c = int(input("Enter a number: "))

print(c == 3 and 5 or c == 5 and 3)

Solution 2:[2]

another thing you could try is using the tuple ternary using the (when_false, when_true)[condition] logic:

    x = int(input("Please enter a number: "))
    change_integer = (3, 5)[x == 3]
    print(change_integer)

Returns 5 when input is 3 (as the condition is 'True') & returns a 3 when input is 5 (as the condition is 'False'). Note that it will always return a 3 unless the input is 5, so this will only be useful if you only accept the two variables 3 and 5.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Geeky Quentin
Solution 2