Pokerboy
Pokerboy

Reputation: 27

Python 3.x function incorrect value

I can't understand why my code is giving me the wrong value.. As a beginner on Python I'm learning def().

So my code is:

def secret_formula(started):

  jelly_beans = started * 500
  jars = jelly_beans / 1000
  crates = jars / 100
  return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

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

start_point = start_point / 10

print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))

When I run my program my answer is:

With a starting point of: 10000

We'd have 5000000 beans, 5000.0 jars,and 50.0 crates.

We can have also do that this way:

We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.

But I believe that should be:

With a starting point of: 10000

We'd have 5000000 beans, 5000.0 jars,and 50.0 crates.

We can have also do that this way:

We'd have 500000 beans, 500 jars, and 5 crates.

Something like that... Because of start_point = start_point / 10 right?

What I am doing wrong?

Obs: Yes I used different methods to "print" for testing reasons.

Upvotes: 1

Views: 61

Answers (3)

Eplox
Eplox

Reputation: 151

Value of beans, jars, crates has already been assigned by the first time you run secret_formula(). Changing start_point will update the value of the variable "start_point", not the others.

By running the secret_formula() again, you will get the new values you are looking for.

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


start_point = 10000
beans, jars, crates = secret_formula(start_point)

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

start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)

print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))

Upvotes: 2

user1372408
user1372408

Reputation: 436

Changing the value of starting_point does not mean the values of beans, crates and jars is recalculated. You have to reassign them again to get the right values.

Try repeating the line where you call the secret formula after changing the value of starting_point.

Upvotes: 1

Stephen Rauch
Stephen Rauch

Reputation: 49774

You need to recall secret_formula() like:

start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)

Test Code:

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

start_point = 10000
beans, jars, crates = secret_formula(start_point)

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

start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)

print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars,
                                                           crates))

Results:

With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can have also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.

Upvotes: 2

Related Questions