[英]How do I use raw_input with classes in a definition
I'm writing a script in Python 2.7 that has the user input the atomic symbol for an element. 我正在Python 2.7中编写一个脚本,让用户输入元素的原子符号。 The script then prints out information about the element.
然后,脚本将打印出有关元素的信息。
However, I'm not sure how to have a class use a variable from the raw_input
. 但是,我不确定如何让类使用
raw_input
的变量。 Here is the code with a couple of the 118 elements gone for readability: 以下是具有118个元素以提高可读性的代码:
class PTable(object):
def __init__(self, name, atom_num, atom_sym, atom_mass,period, group, atom_type,state):
self.name = name
self.atom_num = atom_num
self.atom_sym = atom_sym
self.atom_mass = atom_mass
self.period = period
self.group = group
self.atom_type = atom_type
self.state = state
h = PTable("Hydrogen",1,"H",1.0079,1,1,"Nonmetal","Gas")
he = PTable("Helium",2,"He",4.0026,1,18,"Nonmetal","Gas")
li = PTable("Lithium",3,"Li",6.941,2,1,"Alkali metal","Solid")
be = PTable("Beryllium",4,"Be",9.0121831,2,2,"Alkaline earth","solid")
og = PTable("Oganesson",1,"H",1.008,1,1,"Nonmetal","Gas")
def results(name, num, sym, mass, per, gro, typ, state):
print "Name:", name
print "Atomic number:", num
print "Atomic symbol:", sym
print "Atomic mass:", mass
print "Period:", per
print "Group:", gro
print "Type:", typ
print "State:", state
# results(h.name, h.atom_num, h.atom_sym, h.atom_mass, h.period, h.group, h.atom_type, h.state)
def hub():
x = raw_input("What element? ")
results(%s.name, %s.atom_num, %s.atom_sym, %s.atom_mass, %s.period, %s.group, %s.atom_type, %s.state) % (x)
hub()
hub()
The code that gives me the syntax error is: 给我语法错误的代码是:
results(%s.name, %s.atom_num, %s.atom_sym, %s.atom_mass, %s.period, %s.group, %s.atom_type, %s.state) % (x)
The error is obvious; 错误很明显; syntax is wrong, so I tried another way:
语法是错误的,所以我尝试了另一种方法:
results(x.name, x.atom_num, x.atom_sym, x.atom_mass, x.period, x.group, x.atom_type, x.state)
That, too, did not work, and I got the error 那也行不通,我得到了错误
Traceback (most recent call last):
追溯(最近一次通话):
File "C:/Users/NAME/Desktop/PTable.py", line 146, in
文件“ C:/Users/NAME/Desktop/PTable.py”,行146,位于
hub()
File "C:/Users/NAME/Desktop/PTable.py", line 143, in hub
集线器中的文件“ C:/Users/NAME/Desktop/PTable.py”,第143行
results(x.name, x.atom_num, x.atom_sym, x.atom_mass, x.period, x.group, x.atom_type, x.state)
AttributeError: 'str' object has no attribute 'name'
AttributeError:“ str”对象没有属性“ name”
Do you know how I can make it so the user is able to type in the name of the element (the atomic symbol) and the code prints out the information? 您知道我该怎么做,以便用户能够键入元素的名称(原子符号),然后代码将信息打印出来?
The line x = raw_input("What element? ")
provides you with a string, say 'he'
, so when you call x.name
you are attempting to access an attribute of that string and not of the variable he
. x = raw_input("What element? ")
您提供了一个字符串,说'he'
,因此,当您调用x.name
您尝试访问的是该字符串的属性,而不是变量he
的属性。
What you should do is store your elements in a dictionary instead of having them as variables and access them with the key provided by your user. 您应该做的是将元素存储在字典中,而不是将它们作为变量并使用用户提供的键来访问它们。
periodic_table = {
'h': PTable("Hydrogen",1,"H",1.0079,1,1,"Nonmetal","Gas"),
'he': PTable("Helium",2,"He",4.0026,1,18,"Nonmetal","Gas"),
...
}
symbol = raw_input("What element? ")
try:
element = periodic_table[symbol]
except KeyError:
print('This element does not exist')
As for printing the element, I would suggest a more object-oriented approach by implementing the PTable.__str__
method. 至于打印元素,我将通过实现
PTable.__str__
方法来建议一种更加面向对象的方法。
class PTable(object):
...
def __str__(self):
# Add in the format and information that you want to be printed
return "Name: {}".format(self.name)
You can then directly print your elements. 然后,您可以直接打印您的元素。
print periodic_table['he']
# prints: 'Name: Helium'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.