#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목[re]9/29 Lecture7 excercise 관련 질문2022-10-04 16:59
작성자

​여러 방법으로 해결할 수 있겠지만, 간단한 두 가지 방법을 소개해드립니다.


1. 파일을 While문 안에서 열기


i=open("Exercise.txt","w")

ID=["myname\n", "1234"]

i.writelines(ID)

i.close()


while True:

    with open("./Exercise.txt","r") as k:

        name=input("input your name: ")

        password=input("input your password: ")

        if k.readline()[:-1]==name and k.readline()==password:

            print("Success!")

            break

        else:

            print("Fail!")


print("The End")


2. seek(n) 함수 사용하기. https://blog.naver.com/jkg57/222120795434 참고. 수업시간에 배운 내용은 아니지만 이런 방법도 있습니다.

i=open("Exercise.txt","w")

ID=["myname\n", "1234"]

i.writelines(ID)

i.close()

k=open("./Exercise.txt","r")


while True:

    k.seek(0). # read를 하더라도 처음부터 읽도록 다시 파일의 처음위치(0)으로 돌아가기.

    name=input("input your name: ")

    password=input("input your password: ")

    if k.readline()[:-1]==name and k.readline()==password:

        print("Success!")

        break

    else:

        print("Fail!")


print("The End")

댓글