Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/elif help
#1
Hello all! So I am starting to mess around with python, it's the first language I am learning, I am also learning independently.

I decided to try and make a program using the periodic table where one can look up any element given either the name, symbol, atomic number or standard atomic weight.

Everything was going pretty well, I won't lie I've got around 4 hours in this code, however for my final elif the program won't use the element.mass() to identify the element.

Instead it returns an error stating AttributeError: 'periodictable' has no attribute 'mass' however I use element.mass in previous parts of the code to print the standard atomic weight so I'm confused.

Any insights? I've included my code below.

from periodictable import *
print("Please enter one of the following; \n1:name \n2:symbol \n3:atomic number \n4:Standard Atomic Weight\n")
inquiry = int(input("How will you be searching for elements today? :"))
if inquiry == 1:
  print("\nPlease use lowercase letters")
  ele_name = input("\nWhat is the name of the element? :")
  print("\nSymbol: %s\n\nStandard Atomic Weight: %s\n\nAtomic Number: %s"%(elements.name(ele_name),elements.name(ele_name).mass, elements.name(ele_name).number))
  #sys.exit()
elif inquiry == 2: 
  print("\nRemember to capitalize thr first letter!")
  ele_sym = input("\nWhat is the element's symbol? :")
  print("\nName: %s \n\nStandard Atomic Weight: %s \n\nAtomic Number: %s"%(elements.symbol(ele_sym).name,elements.symbol(ele_sym).mass,elements.symbol(ele_sym).number))
  #sys.exit
elif inquiry == 3:
  ele_num = input("\nWhat is the atomic number you're looking for? :")
  print("\nSymbol: %s\n\nName: %s\n\nStandard Atomic Weight: %s"%    (elements[int(ele_num)],elements[int(ele_num)].name,elements[int(ele_num)].mass))
    #sys.exit()
elif inquiry == 4:
  SAW = float(input("\nWhat is the element's Standard Atomic Weight? :"))
  print("\n%s"%(elements.mass(SAW).name))
Reply
#2
You do not use elements.mass anywhere else. You use elements[index].mass. One is a collection, the other an item from the collection.
Reply
#3
(May-03-2022, 11:21 PM)deanhystad Wrote: You do not use elements.mass anywhere else. You use elements[index].mass. One is a collection, the other an item from the collection.

Ok, but wouldn't element.name() / element.symbol() also be items from the collection? So why does element.name().mass or element.symbol().name pull the information I am looking for and element.mass().name does not?
Reply
#4
From the core.py source:
Quote:Elements are accessed from a periodic table using table[number],
table.name or table.symbol where *symbol* is the two letter symbol.
Individual isotopes are accessed using element[isotope]. Individual ions
are references using element.ion[charge]. Note that
element[isotope].ion[charge].mass will depend on the particular charge
since we subtract the charge times the rest mass of the electron from the
overall mass.
So you can access elements by number, name or symbol. It does not look like they support accessing elements by mass.
stupidanlearning likes this post
Reply
#5
Hey thankyou! Is that from the compressed source tarball?
Reply
#6
I went to pypl and clicked on the link to the project homepage.

pypl: https://pypi.org/project/periodictable/

homepage: https://github.com/pkienzle/periodictable
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,256 Jul-28-2019, 05:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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