You are on page 1of 2

import pandas as pd

import matplotlib.pyplot as plt

# Create a DataFrame for the initial periodic table


periodic_table_data = {
'Symbol': ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'],
'Name': ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon',
'Nitrogen', 'Oxygen', 'Fluorine', 'Neon'],
'Atomic Number': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'Atomic Mass': [1.008, 4.0026, 6.94, 9.0122, 10.81, 12.011, 14.007, 15.999,
18.998, 20.180],
}

periodic_table = pd.DataFrame(periodic_table_data)

def search_element_by_name(name):
name = name.lower() # Convert user input to lowercase
element_info =
periodic_table[periodic_table['Name'].str.lower().str.contains(name)]
if not element_info.empty:
print(element_info.to_string(index=False))
else:
print(f"No elements found with the name '{name}'.")
def add_element(symbol, name, atomic_number, atomic_mass):
global periodic_table # Declare that we are using the global variable
new_element = pd.DataFrame({'Symbol': [symbol], 'Name': [name], 'Atomic
Number': [atomic_number], 'Atomic Mass': [atomic_mass]})
periodic_table = pd.concat([periodic_table, new_element], ignore_index=True)
print(f"Element '{name}' with symbol '{symbol}' added successfully.")

def delete_element(symbol):
if symbol in periodic_table['Symbol'].values:
periodic_table.drop(periodic_table[periodic_table['Symbol'] ==
symbol].index, inplace=True)
print(f"Element with symbol '{symbol}' deleted successfully.")
else:
print(f"Element with symbol '{symbol}' not found.")

def update_element_info(symbol):
if symbol in periodic_table['Symbol'].values:
print("Enter the updated information for the element:")
name = input("Name: ")
atomic_number = int(input("Atomic Number: "))
atomic_mass = float(input("Atomic Mass: "))

periodic_table.loc[periodic_table['Symbol'] == symbol, ['Name', 'Atomic


Number', 'Atomic Mass']] = [name, atomic_number, atomic_mass]
print(f"Element with symbol '{symbol}' updated successfully.")
else:
print(f"Element with symbol '{symbol}' not found.")

def display_periodic_table():
print("\nPeriodic Table:")
print(periodic_table.to_string(index=False))

def visualize_periodic_table():
plt.figure(figsize=(10, 6))

plt.bar(periodic_table['Name'], periodic_table['Atomic Mass'],


color='skyblue')
plt.xlabel('Element Name')
plt.ylabel('Atomic Mass')
plt.title('Atomic Mass of Elements in the Periodic Table')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

# ... (previous functions)

if __name__ == "__main__":
while True:
print("\nPeriodic Table Program:")
print("1. Enter the symbol of an element to get information.")
print("2. Add a new element to the periodic table.")
print("3. Delete an element from the periodic table.")
print("4. View the entire periodic table.")
print("5. Search for elements by name.")
print("6. Update information of an existing element.")
print("7. Visualize the periodic table.")
print("8. Type 'exit' to end the program.")

user_choice = input("Enter your choice (1-8): ")

if user_choice.lower() == 'exit':
print("Exiting the program. Goodbye!")
break

if user_choice == '1':
user_input = input("Enter element symbol: ")
search_element_by_name(user_input.upper())
elif user_choice == '2':
symbol = input("Enter the symbol of the new element: ")
name = input("Enter the name of the new element: ")
atomic_number = int(input("Enter the atomic number of the new
element: "))
atomic_mass = float(input("Enter the atomic mass of the new element:
"))
add_element(symbol, name, atomic_number, atomic_mass)
elif user_choice == '3':
symbol = input("Enter the symbol of the element to delete: ")
delete_element(symbol)
elif user_choice == '4':
display_periodic_table()
elif user_choice == '5':
name = input("Enter part or full name of the element to search: ")
search_element_by_name(name)
elif user_choice == '6':
symbol = input("Enter the symbol of the element to update: ")
update_element_info(symbol)
elif user_choice == '7':
visualize_periodic_table()
else:
print("Invalid choice. Please enter a number between 1 and 8.")

You might also like