import pubchempy as pcp
# Take name as input
chemical_name = input("Enter chemical name: ")
try:
# Search PubChem for the compound by its name
compound = pcp.get_compounds(chemical_name, 'name')[0]
# Display information about the compound
print(f"IUPAC Name: {compound.iupac_name}")
print(f"Common Name: {compound.synonyms[0]}")
print(f"Molecular Weight: {compound.molecular_weight}")
print(f"Formula: {compound.molecular_formula}")
# You can access more properties as needed
except IndexError:
print(f"No information found for {chemical_name}.")
#clcoding.com
Explanation:
This code snippet performs a similar task to the previous one but takes the name of a chemical compound as input instead of its chemical formula. Here's an explanation of each part:
import pubchempy as pcp: This line imports the PubChemPy library and aliases it as pcp, allowing us to refer to it more conveniently in the code.
chemical_name = input("Enter chemical name: "): This line prompts the user to input the name of the chemical compound they want to retrieve information about. The input is stored in the variable chemical_name.
try:: This starts a try-except block, indicating that we are going to try a piece of code that might raise an exception, and if it does, we'll handle it gracefully.
compound = pcp.get_compounds(chemical_name, 'name')[0]: This line uses the get_compounds function from the PubChemPy library to search for compounds matching the provided chemical name. The function takes two arguments: the chemical name (chemical_name) and a string indicating that we are searching by name ('name'). Since get_compounds returns a list of compounds, we select the first compound using [0] and assign it to the variable compound.
Printing compound information:
print(f"IUPAC Name: {compound.iupac_name}"): This line prints the IUPAC (International Union of Pure and Applied Chemistry) name of the compound.
print(f"Common Name: {compound.synonyms[0]}"): This line prints the common name of the compound, using the first synonym available.
print(f"Molecular Weight: {compound.molecular_weight}"): This line prints the molecular weight of the compound.
print(f"Formula: {compound.molecular_formula}"): This line prints the molecular formula of the compound.
except IndexError:: This block catches the IndexError exception, which occurs if no compound is found for the provided chemical name.
print(f"No information found for {chemical_name}."): If an IndexError occurs, this line prints a message indicating that no information was found for the provided chemical name.
In summary, this code allows the user to input the name of a chemical compound, searches for the corresponding compound in the PubChem database using PubChemPy, and displays information about the compound if found. If no information is found for the provided name, it prints a corresponding message.
import pubchempy as pcp
# Define the chemical formula
chemical_formula = input("Enter chemical Formula : ")
try:
# Search PubChem for the compound by its chemical formula
compound = pcp.get_compounds(chemical_formula, 'formula')[0]
# Display information about the compound
print(f"IUPAC Name: {compound.iupac_name}")
print(f"Common Name: {compound.synonyms[0]}")
print(f"Molecular Weight: {compound.molecular_weight}")
print(f"Formula: {compound.molecular_formula}")
# You can access more properties as needed
except IndexError:
print(f"No information found for {chemical_formula}.")
#clcoding.com
Explantion:
This code snippet aims to retrieve information about a chemical compound using its chemical formula. Here's a breakdown of each part:
import pubchempy as pcp: This line imports the PubChemPy library and aliases it as pcp, allowing us to refer to it more conveniently in the code. PubChemPy is a Python library for accessing chemical information from the PubChem database.
chemical_formula = input("Enter chemical Formula : "): This line prompts the user to input the chemical formula of the compound they want to retrieve information about. The input is stored in the variable chemical_formula.
try:: This starts a try-except block, indicating that we are going to try a piece of code that might raise an exception, and if it does, we'll handle it gracefully.
compound = pcp.get_compounds(chemical_formula, 'formula')[0]: This line uses the get_compounds function from the PubChemPy library to search for compounds matching the provided chemical formula. The function takes two arguments: the chemical formula (chemical_formula) and a string indicating that we are searching by formula ('formula'). Since get_compounds returns a list of compounds, we select the first compound using [0] and assign it to the variable compound.
Printing compound information:
print(f"IUPAC Name: {compound.iupac_name}"): This line prints the IUPAC (International Union of Pure and Applied Chemistry) name of the compound.
print(f"Common Name: {compound.synonyms[0]}"): This line prints the common name of the compound, using the first synonym available.
print(f"Molecular Weight: {compound.molecular_weight}"): This line prints the molecular weight of the compound.
print(f"Formula: {compound.molecular_formula}"): This line prints the molecular formula of the compound.
except IndexError:: This block catches the IndexError exception, which occurs if no compound is found for the provided chemical formula.
print(f"No information found for {chemical_formula}."): If an IndexError occurs, this line prints a message indicating that no information was found for the provided chemical formula.
In summary, this code allows the user to input a chemical formula, searches for the corresponding compound in the PubChem database using PubChemPy, and displays information about the compound if found. If no information is found for the provided formula, it prints a corresponding message.
0 Comments:
Post a Comment