我正在使用Pycharm来运行它,因为这是我们教授的要求
元素周期表的开发是为了组织有关组成宇宙的元素的信息。编写一个python程序,让您输入有关元素周期表中每个元素的信息。 确保您包括以下信息: 符号,名称,原子序数,行和列 提供选项菜单供用户执行以下操作: 通过输入该元素的符号,查看有关该元素存储的所有信息。 选择一个属性,然后查看表中每个元素的属性。
我有以下代码
class element:
def init(self, sym, name, no, typ, row, col):
self.symbol = sym
self.name = name
self.atomic_number = no
self.elem_type = typ
self.row = row
self.column = col
def viewElement(element, n):
print("Name: " + element.name)
if n == 2 or n == 3:
print("Symbol: " + element.symbol)
if n == 2 or n == 4:
print("Atomic Number: " + str(element.atomic_number))
if n == 2 or n == 5:
print("Type: " + element.elem_type)
if n == 2 or n == 6:
print("Row: " + str(element.row))
if n == 2 or n == 7:
print("Column: " + str(element.column))
print("\n")
if __name__ == "__main__":
elem_dict = {}
while 1:
print("\n Main Menu \n")
print("1. Add an Element")
print("2. View an Element")
print("3. Viel all Element Names")
print("4. View all Atomic Numbers")
print("5. View all Types")
print("6. View all Row Numbers")
print("7. View all Column Number")
print("8. Exit")
choice = int(input("Enter your Choice: "))
if choice == 1:
sym = input("Enter the symbol: ")
name = input("Enter the element name: ")
atom = int(input("Enter the atomic number: "))
typ = input("Enter the element type: ")
row = int(input("Enter the row number: "))
col = int(input("Enter the column number: "))
if sym not in elem_dict.keys():
e1 = element(sym, name, atom, typ, row, col)
elem_dict[sym] = e1
print(sym + " added\n")
else:
print("Element already present")
elif choice == 2:
sym = input("Enter the symbol: ")
viewElement(elem_dict[sym], 2)
elif choice == 3:
for i in elem_dict.keys():
viewElement(elem_dict[i], 3)
elif choice == 4:
for i in elem_dict.keys():
viewElement(elem_dict[i], 4)
elif choice == 5:
for i in elem_dict.keys():
viewElement(elem_dict[i], 5)
elif choice == 6:
for i in elem_dict.keys():
viewElement(elem_dict[i], 6)
elif choice == 7:
for i in elem_dict.keys():
viewElement(elem_dict[i], 7)
elif choice == 8:
print("Exiting")
break
else:
print("Wrong entry, Please re-enter\n")
选择选项1并得到最后一部分时,我不断收到以下错误
主菜单
输入您的选择:1
输入符号:H
输入元素名称:氢气
输入原子序数:1
输入元素类型:Gas
输入行号:1
输入列号:1
Traceback (most recent call last):
File "C:/Users/stephanie/Desktop/LEGRANGE_UNIT_SEVEN/Periodic_Table.py",
line 51, in <module>
e1 = element(sym, name, atom, typ, row, col)
TypeError: element() takes no arguments
Process finished with exit code 1
答案 0 :(得分:1)
您需要将init
方法更改为弱点(__init__
):
def __init__(self, sym, name, no, typ, row, col):
# Notice the double underscore either side of init
否则,该类将不会初始化,因为您的init
只是该类内部的一个自定义方法。