由于最近对信息安全和网络编程感兴趣,我决定通过 Internet 学习 Python。我正在尝试编写一段代码,允许用户存储所需人数的生物数据(姓名、年龄和工作)。然后 O/P 将包括所有这些人的生物数据以及人数。作为 Python 新手,我无法识别错误。谢谢 - 席德
这是我的代码:
#!/usr/bin/python
class Bio:
counts = 0
myBio = []
def __init__(self, name, age, job):
Bio.myBio[Bio.counts] = name
Bio.myBio[Bio.counts+1] = age
Bio.myBio[Bio.counts+2] = job
Bio.counts + 1
def display(self):
for myBio in range(0, Bio.counts):
print myBio
while 1:
name = raw_input("Enter your name: ")
age = int(raw_input("Enter your age: "))
job = raw_input("Enter your Job: ")
details = Bio(name, age, job)
details.display()
print "Detail Count %d" % Bio.myBio
anymore = raw_input("Anymore details ?: (y/n)")
if anymore == 'n':
break
这是我的 O/P 的踪迹:
./bio.py
Enter your name: Sid
Enter your age: 21
Enter your Job: InfoSec
Traceback (most recent call last):
File "./bio.py", line 25, in <module>
details = Bio(name, age, job)
File "./bio.py", line 9, in __init__
Bio.myBio[Bio.counts] = name
IndexError: list assignment index out of range*