print("Hello and welcome to your address book this program uses surnames or D.O.B to find people in the address book file.")
yn = ""

while yn != "n":
    yn = input ("Would you like to search for a user? (Y/N) ")

    if yn == "y":
        search = input ("Would you like to search by surname (S) or month of birth (M) ")

        if search.lower() == "s":
            surname = input ("Please enter the surname: ")
            for line in open("datafile.txt"):
                if surname in line:
                    print(line)


        elif search.lower() == "m":
            DMY = input("please enter your date of birth you are looking for (date/month/year) : ")
            DMY = DMY.split("/")
            DMY = DMY[1]

            for line in open("datafile.txt"):
                if DMY in line:
                    print(line)
        else:
            print ("Sorry you can not do this please try again.")
    elif yn == "n":
        print("Goodbye")
    else:
        print("Sorry you can not do this please try again.")

最佳答案

一种简单的方法是将两个字符串都转换为小写。

更改

if surname in line:




if surname.lower() in line.lower():

关于python - 如果您使用大写和小写作为搜索名称,则我需要此方法(大写和小写使用不同的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21407974/

10-11 23:09
查看更多