Python Forum

Full Version: What is wrong with my chemistry code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys, can someone explain me why it's wrong and it doesn't work. I'm beginner.

def product_produced(actual_substance): 
    def yield_perc(percentage_yield):
        actual = input("Enter grams of actual_substance")
        py = input("Enter percent yield: ")
        M_reactant = input("Enter MOLAR MASS of reactant: ") 
        M_product = input("Enter MOLAR MASS of product: ")
        coef_reactant = input("Enter coefficient of reactant: ")
        coef_product = input("Enter coefficient of reactant: ") 
        theoretical_substance = (actual * 100)/py
        reactant_need = (coef_reactant * M_reactant * theoretical_substance)/(coef_product* M_product) 
        print(reactant_need) 
    yield_perc(py)
product_produced(actual)
Sorry i used _ instead of tab because i dont know how to put tab here.
Hello,
instead of tabs use [python] code tags when inserting code. You can find help here:
https://python-forum.io/misc.php?action=help&hid=25

Also you will need to be more specific about what it means that code doesn't work and it's wrong.
If you get an error, post full error traceback in error tags. If result is not as you expected, post the actual and desired result (you can use output tags).
You are using py as an argument to yeild_perc on line 12. But py is defined inside the yield_perc function. You can't access values within a function without returning them, and certainly not before/as the function is actually called.

I would just remove that arguments/parameter from the yeild_perc function, since it is defined in the function with an input statement. But you will have other problems, because input returns a string. You need to convert those values to numbers with float() or int() before you do math on them.
(Dec-29-2019, 06:00 PM)ichabod801 Wrote: [ -> ]You are using py as an argument to yeild_perc on line 12. But py is defined inside the yield_perc function. You can't access values within a function without returning them, and certainly not before/as the function is actually called. I would just remove that arguments/parameter from the yeild_perc function, since it is defined in the function with an input statement. But you will have other problems, because input returns a string. You need to convert those values to numbers with float() or int() before you do math on them.


Could you please correct this function part ?
You try to correct it. I help you if it doesn't work.
I tried actually, there was error again. It's because my english level i cant exactly catch what you mean, i thought if you would show me on code it will be better for me

(Dec-29-2019, 07:39 PM)ichabod801 Wrote: [ -> ]You try to correct it. I help you if it doesn't work.
i understood this float, int part, only problem is function
Show me your current code, and the full text of the error.
#!/usr/bin/env python3 
actual = input("Enter grams of actual_substance: ")
percent  = input("Enter percent yield: ")
def actual_yield(reactant_need): 
  M_reactant = input("Enter MOLAR MASS of reactant: ") 
  M_product = input("Enter MOLAR MASS of product: ")
  coef_reactant = input("Enter coefficient of reactant: ")
  coef_product = input("Enter coefficient of product: ") 
  theoretical_substance = (actual * 100)/percent
  reactant_need = (coef_reactant * M_reactant * theoretical_substance)/(coef_product* M_product) 
  return reactant_need
print(actual_yield(reactant_need))
errors:
Enter grams of actual_substance: 19.2
Enter percent yield: 80
Traceback (most recent call last):
File "reaction.py", line 12, in <module>
print(actual_yield(reactant_need))
NameError: name 'reactant_need' is not defined
This is the exact same problem, you can't use something defined in a function outside of the function. That's what you are doing on line 12. To simplify, you are doing this:

def foo(bar):
    bar = 23
    return bar
foo(bar)
Which causes an error, because bar is defined in the function (on line 2), and is being used outside the function (on line 4). The fix is simple in your case:

def foo():
    bar = 23
    return bar
bar = foo()
(Dec-30-2019, 03:27 PM)ichabod801 Wrote: [ -> ]Show me your current code, and the full text of the error.

#!/usr/bin/env python3 
actual = input("Enter grams of actual_substance: ")
percent  = input("Enter percent yield: ")
def actual_yield(reactant_need): 
  M_reactant = input("Enter MOLAR MASS of reactant: ") 
  M_product = input("Enter MOLAR MASS of product: ")
  coef_reactant = input("Enter coefficient of reactant: ")
  coef_product = input("Enter coefficient of product: ") 
  theoretical_substance = (actual * 100)/percent
  reactant_need = (coef_reactant * M_reactant * theoretical_substance)/(coef_product* M_product) 
  return reactant_need
print(actual_yield(reactant_need))
errors:
Enter grams of actual_substance: 19.2
Enter percent yield: 80
Traceback (most recent call last):
File "reaction.py", line 12, in <module>
print(actual_yield(reactant_need))
NameError: name 'reactant_need' is not defined

(Dec-30-2019, 06:42 PM)ichabod801 Wrote: [ -> ]This is the exact same problem, you can't use something defined in a function outside of the function. That's what you are doing on line 12. To simplify, you are doing this:
 def foo(bar): bar = 23 return bar foo(bar) 
Which causes an error, because bar is defined in the function (on line 2), and is being used outside the function (on line 4). The fix is simple in your case:
 def foo(): bar = 23 return bar bar = foo() 

Yes! THank you very much, I understood!!!!!!!!