-3

The function part of this program (everything after def) is not working. I have no error messages, and I believe I've typed everything the way Zed did. What's wrong?

print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tell'
# newline and a space are put in, tab and a space are put in

# this doesn't print the poem, only stores it
poem = """
\tThe lovely world 
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\t where there is none
"""

print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 -6
print "This should be five: %s" % five

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates
    # these are local variables, they are inside the function

    start_point = 10000
    # whatever happened to started now happens to start point
    # these are global variables, they are outside the function, could be called x, y, and z
    beans, jars, crates = secret_formula(start_point)

    print "With a starting point of: %d" % start_point
    print "We'd have %d beans, %d jars, and %d crates." (beans, jars, crates)

    # reassigns with new value based on old value
    start_point = start_point / 10

    print "We can also do that this way:"
    # brings down the info from line 29
    print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

2

Your code never calls the function. And that's because you put the code that should call it too far indented.

Your function should only be defined as this:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates
    # these are local variables, they are inside the function

Everything after those lines should not be indented:

start_point = 10000
# whatever happened to started now happens to start point
# these are global variables, they are outside the function, could be called x, y, and z
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

# reassigns with new value based on old value
start_point = start_point / 10

print "We can also do that this way:"
# brings down the info from line 29
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

Note that this code should all start at the beginning of the line, with no spaces between the start of the line and the instructions on each line.

I also corrected a typo on one of those lines. You typed:

print "We'd have %d beans, %d jars, and %d crates." (beans, jars, crates)

This tries to call the string (the (...) comes directly after the "..." string literal). You are missing the % operator on that line; put that in too for the formatting operation to work:

print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I typed in "secret_formula(five)" and nothing happened. No error message either. I tried using a rewrite of this code from another stackoverflow question and it only gave me error messages about indentation. def change(start_point): start_point[0] = 3 start_point = [1] change(start_point) print start_point http://stackoverflow.com/questions/15148496/python-passing-an-integer-by-reference – Chris Trachte Jun 11 '16 at 23:30
  • @ChrisTrachte: that post has nothing to do with your question; you really don't need to go there. The code you posted *mostly* works except for the problems I pointed out. – Martijn Pieters Jun 11 '16 at 23:34
  • Ok, got it. It worked after removing the indents for the code that wasn't part of the function, and after adding the formatting operator %. – Chris Trachte Jun 11 '16 at 23:37