我正在尝试打印变量'Number'但是它出现了错误。
'TypeError:列表索引必须是整数,而不是元组'
我不明白?什么是元组,我怎么能解决这个问题? 如何将文本文件中的行号分配给变量?
def CheckDatabase():
print ("====Check Database===== \nA)Would you like to check details? \nB)Check if they are a member?")
TC = input(": ")
if TC == "A" or TC == "a":
NameDetail = input ("Please enter the name you would like to check the details of.\nThis will only work if they are a member\n: ")
with open('Name.txt') as ND:
for number, line in enumerate(ND, 1):
if (str(NameDetail)) in line:
Number = number, line in enumerate(ND, 1)
print ("\nName = ", NameDetail)
A = open("Address.txt", "r")
AData =[line.rstrip() for line in A.readlines()]
print ("Address = ",(AData[Number]))
HN = open("Home Number.txt", "r")
HNData =[line.rstrip() for line in HN.readlines()]
print ("Home Number = ",(HNData[Number]))
MN = open("Mobile Number.txt", "r")
MNData =[line.rstrip() for line in MN.readlines()]
print ("Mobile Number = ",(MNData[Number]))
EAS = open("Email Address.txt", "r")
EAData =[line.rstrip() for line in EAS.readlines()]
print ("Email Address = ",(EAData[Number]))
MDND = open("Email Address.txt", "r")
MDNData =[line.rstrip() for line in MDND.readlines()]
print ("Medical/Dietry Needs = ",(MDNData[Number]))
else:
print ("Person not found!")
修改
import time
def AddToDatabase():
print ("\nYou are adding to the Database. \nA)Continue \nB)Go Back")
ATD = input(": ")
if ATD == "A" or ATD == "a":
Name = input("\nEnter Name of Member [First Name and Surname]: ")
with open("Name.txt", "a") as N:
N.write("\n{}".format(Name))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
print ("\nEnter Address of "+Name+" all on one line")
print ("In format [Include Commas]")
print ("\nRoad name with house number [e.g. 1 Morgan Way], Borough [e.g Harrow], City [e.g London], Postcode [e.g. HA5 2EF]")
Address = input("\n: ")
with open("Address.txt", "a") as A:
A.write("\n{}".format(Address))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
Home_Number = input("\nEnter Home Number of "+Name+": ")
with open("Home Number.txt", "a") as HN:
HN.write("\n{}".format(Home_Number))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
Mobile_Number = input ("\nEnter Mobile Number of "+Name+": ")
with open("Mobile Number.txt", "a") as MN:
MN.write("\n{}".format(Mobile_Number))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
Email_Address = input ("\nEnter Email Address of "+Name+": ")
with open("Email Address.txt", "a") as EA:
EA.write("\n{}".format(Email_Address))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
Dietry_Needs = input("\nEnter Medical/Dietry Needs of "+Name+": ")
with open("Medical Dietry Needs.txt", "a") as MDN:
MDN.write("\n{}".format(Dietry_Needs))
time.sleep(1)
print ("\nAdding...")
time.sleep(1)
print ("All information for "+Name+" has been added. Redirecting Back To Main...")
time.sleep(5)
Main()
elif ATD == "B" or ATD == "b":
Main()
def CheckDatabase():
print ("====Check Database===== \nA)Would you like to check details? \nB)Check if they are a member?")
TC = input(": ")
if TC == "A" or TC == "a":
NameDetail = input ("Please enter the name you would like to check the details of.\nThis will only work if they are a member\n: ")
with open('Name.txt') as ND:
for number, line in enumerate(ND, 1):
if (str(NameDetail)) in line:
Number, junk = number, line in enumerate(ND, 1)
print ("\nName = ", NameDetail)
A = open("Address.txt", "r")
AData =[line.rstrip() for line in A.readlines()]
print ("Address = ",(AData[number]))
HN = open("Home Number.txt", "r")
HNData =[line.rstrip() for line in HN.readlines()]
print ("Home Number = ",(HNData[Number]))
MN = open("Mobile Number.txt", "r")
MNData =[line.rstrip() for line in MN.readlines()]
print ("Mobile Number = ",(MNData[Number]))
EAS = open("Email Address.txt", "r")
EAData =[line.rstrip() for line in EAS.readlines()]
print ("Email Address = ",(EAData[Number]))
MDND = open("Email Address.txt", "r")
MDNData =[line.rstrip() for line in MDND.readlines()]
print ("Medical/Dietry Needs = ",(MDNData[Number]))
else:
print ("Person not found!")
elif TC == "B" or TC == "b":
NameChoice = input("Enter persons name: ")
with open('Name.txt') as NAME:
for number, line in enumerate(NAME, 1):
if (str(NameChoice)) in line:
print (NameChoice)
print ("is a member")
else:
print ("Not a member!")
def Main():
print ("\nSVA of UK Database")
while True:
print ("\nA)Check Database \nB)Add to Database \nC)Exit Program")
choice = input(": ")
if choice == "A" or choice == "a":
CheckDatabase()
elif choice == "B" or choice == "b":
AddToDatabase()
elif choice == "C" or choice == "c":
break
else:
print ("Invalid Input")
Main()
Main()
编辑2: Name.txt:
Sagar Bharadia
address.txt中
8 John Road
Home Number.txt
02089563524
Mobile Number.txt
02045745854
Medical Dietry Needs.txt
None
编辑3:
SVA of UK Database
A)Check Database
B)Add to Database
C)Exit Program
: A
====Check Database=====
A)Would you like to check details?
B)Check if they are a member?
: A
Please enter the name you would like to check the details of.
This will only work if they are a member
: Sagar Bharadia
Name = Sagar Bharadia
1
Traceback (most recent call last):
File "H:\SVA UK PROGRAM\SVA of UK.py", line 126, in <module>
Main()
File "H:\SVA UK PROGRAM\SVA of UK.py", line 114, in Main
CheckDatabase()
File "H:\SVA UK PROGRAM\SVA of UK.py", line 74, in CheckDatabase
print ("Address = ",(AData[Number]))
IndexError: list index out of range
>>>
答案 0 :(得分:2)
当您执行Number
时,将Number = number, line in enumerate(ND, 1)
定义为元组。无论如何,您已使用number, line in enumerate(ND, 1)
循环创建了for
。为什么不用它呢?即AData[number]
。
仅使用一个示例,这就是您将Number
设置为:
>>> for x, y in enumerate([1,2,3], 1):
print x, y in enumerate([1,2,3], 1)
1 False
2 False
3 False
这是一个元组,其中包含number
循环中的for
和line in enumerate(ND, 1)
的值(一个布尔表达式,无论是在那里还是它'&#39;不是)。
您还有其他几个问题:
number
提供1
,您可以0
而不是1
开始enumerate
。在python中,列表索引从0
开始。因此,如果列表的长度为1
,那么[1]
处的子索引实际上是超出范围的。您应该enumerate(ND)
而是从0
开始。.close()
您open()
的文件,或使用with
/ as
,就像使用第一个文件一样。Email Address.txt
......你的意思是这样做吗?答案 1 :(得分:0)
在这一行:
Number = number, line in enumerate(ND, 1)
您要为Number分配两个值 - 这会产生一个元组。
你可以这样做:
Number, junk = number, line in enumerate(ND, 1)
这会导致Number为单个值,不再是元组。
答案 2 :(得分:0)
元组是一种&#34;列表&#34;。请参阅doc:
http://docs.python.org/release/1.5.1p1/tut/tuples.html
您可能为var Number
分配了2个或更多值