3

I've got some code:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

start_point = 10000

#print ('We\'d have {0} beans, {1} jars, and {2} crates.'
        .format(secret_formula(start_point)))

print ('We\'d have %d beans, %d jars, and %d crates.'
        % secret_formula(start_point))

My question is regarding the two last statements. The one that is commented out does not work (returns an index out of range error), but the other one does. Why is that? And how can I make the commented out statement work?

Thanks in advance :)

Lars

1 Answer 1

3

The commented out line should be

print ('We\'d have {0} beans, {1} jars, and {2} crates.'
        .format(*secret_formula(start_point)))

Notice the * before secret_formula(...) -- it tells Python to unpack the result when passing the values to format().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.