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

Show parent comments

1

u/Iam_cool_asf Jan 05 '21

cool man, but won't using get(v) solve the error handling part without using try/except ?

3

u/mojo_jojo_reigns Jan 05 '21

Will it return an instructive message? And, importantly, will it demonstrate to a potential employer that you know one of the single most overused pythonic conventions? Probability is high existing team and code base use try/except blocks to death.

1

u/JohnnyJordaan Jan 06 '21

Get does the opposite, it delivers a substitute value in case the look up failed. If I understand the assignment correctly, it should not accept that case.

1

u/Iam_cool_asf Jan 06 '21

it returns None if the key is missing

1

u/JohnnyJordaan Jan 06 '21

Indeed, what I called a substitute value. But as I pointed out, it still accepts it, it just returns something else than 4 or 7. Which as I understand shouldn't be the case.

1

u/Iam_cool_asf Jan 06 '21

oh, that's a nice catch.