Question

Complete the following function in python based on the cutomers request. Function: def hydrocarbon(hydrogen,carbon,oxygen): #Add your...

Complete the following function in python based on the cutomers request.

Function:

def hydrocarbon(hydrogen,carbon,oxygen):

#Add your code here

Customer Request:

take in three parameters assumed to be hydrogen, carbon, and oxygen IN THAT ORDER.

validate that all three values are non-negative integers:

if not than you should return None

if all three parameters are valid then you should calculate the mass of the hydrocarbon and return the mass where:

Each hydrogen has a molecular weight of 1.0079

Each carbon has a molecular weight of 12.011

Each oxygen has a molecular weight of 15.9994

For example:

hydrocarbon(4,3,0) will return 40.0646

hydrocarbon(1,2,3) will return 73.0281

hydrocarbon(-1,-1,-1) will return None

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def hydrocarbon(hydrogen,carbon,oxygen):
if hydrogen < 0 or carbon < 0 or oxygen < 0:
return None
else:
return hydrogen * 1.0079 + carbon * 12.011 + oxygen * 15.9994
  

print(hydrocarbon(4,3,0))
print(hydrocarbon(1,2,3))
print(hydrocarbon(-1 , -1, -1))

Output:

$python3 main.py
40.0646
73.0281
None

ef hydrocarbon(hydrogen, carbon, oxygen): if hydrogeneor carbon<e or oxygen: python3 main.py 18.0646 73.0281 None return None

Add a comment
Know the answer?
Add Answer to:
Complete the following function in python based on the cutomers request. Function: def hydrocarbon(hydrogen,carbon,oxygen): #Add your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT