Fix this code: legal age

This is an example of poorly written code. The code works fine, but it's very ugly. Your job is to refactor it contains the minimum amount of lines (and if statements), while still being readable.

Keep an eye on the only test, and check out the elif statement, it might be useful 😉.

Test Cases

test legal age - Run Test

def test_legal_age():
    assert legal_age(25) == "You can do anything"
    assert legal_age(21) == "You can do anything"
    assert legal_age(19) == "You can drive with parents' permission"
    assert legal_age(18) == "You can drive with parents' permission"
    assert legal_age(16) == "You'll have to wait"
    assert legal_age(8) == "You'll have to wait"

Solution 1

def legal_age(age):
    if age >= 21:
        return "You can do anything"
    elif age >= 18:
        return "You can drive with parents' permission"
    else:
        return "You'll have to wait"
def legal_age(age): if age > 21: return "You can do anything" else: if age == 21: return "You can do anything" else: if age > 18: return "You can drive with parents' permission" else: if age == 18: return "You can drive with parents' permission" else: if age == 17: return "You'll have to wait" else: return "You'll have to wait"