Python Forum
What is wrong with my chemistry code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with my chemistry code?
#1
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.
Reply
#2
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).
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(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 ?
Reply
#5
You try to correct it. I help you if it doesn't work.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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
Reply
#7
Show me your current code, and the full text of the error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
#!/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
Reply
#9
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()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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!!!!!!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 475 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 847 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,551 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,246 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,771 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,527 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,521 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,624 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,493 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,003 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020