1

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.

jdigital
12.4k4 gold badges38 silver badges56 bronze badges
asked May 12, 2018 at 20:36
3
  • 1
    you forgot to call secret_formula() a second time Commented May 12, 2018 at 20:40
  • Changing the value of start_point doesn't affect the other variables! Commented May 12, 2018 at 20:41
  • 1
    I think he didn't forget it. I think he rather assumed that changing the input to that formula would automatically update the results, which obiously does not happen Commented May 12, 2018 at 20:41

3 Answers 3

2

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.
answered May 12, 2018 at 20:40
Sign up to request clarification or add additional context in comments.

1 Comment

So simple! Thank you all!
2

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))
answered May 12, 2018 at 20:43

Comments

1

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.

answered May 12, 2018 at 20:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.