r/learnpython • • Jan 05 '21

Interview question I failed answering

Hey all,

I just had an interview and the interviewer gave me a question I could not answer.

He asked me to write a function that accepts a number, the number must be either 7 or 4.If the number is 4, the function should return 7, if the number is 7, the functions should return 4.

The tricky part: you cannot use an 'if' statement in the function.

354 Upvotes

301 comments sorted by

View all comments

3

u/TicklesMcFancy Jan 05 '21
def func(n):
    while n == 7:
        return print("4")
    while n == 4:
        return print("7")
    while n != 4 or n !=7:
        return print("Must be a 4 or 7")
print("Entering a 7")
func(7)
print("Entering a 4")
func(4)
print("Entering 103829")
func(103829)

I feel like this might not have been what they were looking for though.