입력을 텍스트 파일의 2 줄과 비교하는 코드가 필요합니다. 코드 작성을 시작했으나 작동하지 않습니다. 이제는 3 행에서 잘못되었지만 무엇을해야할지 모르겠습니다.텍스트 파일의 특정 줄을 입력과 비교하십시오.
Name = input("Enter name: ")
with open("numbers") as MyFile:
if line 2 == Name:
print ("correct")
입력을 텍스트 파일의 2 줄과 비교하는 코드가 필요합니다. 코드 작성을 시작했으나 작동하지 않습니다. 이제는 3 행에서 잘못되었지만 무엇을해야할지 모르겠습니다.텍스트 파일의 특정 줄을 입력과 비교하십시오.
Name = input("Enter name: ")
with open("numbers") as MyFile:
if line 2 == Name:
print ("correct")
당신은 readlines
을 사용하고 두 번째 줄 얻을 수 있습니다 : 당신은 '라인 2'를 정의 할 필요가
Name = input("Enter name: ")
with open("numbers") as MyFile:
line2 = MyFile.readlines()[1]
print(Name, line2)
합니다. 시도해보십시오.
name = input("Enter name: ")
with open("numbers") as MyFile:
lines = MyFile.readlines()
if lines[1] == name:
print ("correct")
좀 더 자세한 설명을하려고합니다. 기본 변수를 정의해야합니다. 행 2는 둘 다 변수가 아니며 공간이 있기 때문에 선언되지 않습니다.
name = input("Enter name: ")
with open("numbers") as f:
lines = f.readlines() # a list of all the lines
if lines[1] == name: # the second line (0 indexing)
print ("correct")