提问者:小点点

得到与打印[重复]相同的结果


def dot_trick(username):
    emails = list()
    username_length = len(username)
    combinations = pow(2, username_length - 1)
    padding = "{0:0" + str(username_length - 1) + "b}"
    for i in range(0, combinations):
        bin = padding.format(i)
        full_email = ""

        for j in range(0, username_length - 1):
            full_email += (username[j]);
            if bin[j] == "1":
                full_email += "."
        full_email += (username[j + 1])
        emails.append(full_email + "mars")
    return emails

username = ("marsbros")
print(*dot_trick(username) , sep="\n")

我想得到与sep='\n'的print相同的结果,并将其逐行写入文件。


共1个答案

匿名用户

你可以试试这个

def dot_trick(username):
    emails = list()
    username_length = len(username)
    combinations = pow(2, username_length - 1)
    padding = "{0:0" + str(username_length - 1) + "b}"
    for i in range(0, combinations):
        bin = padding.format(i)
        full_email = ""

        for j in range(0, username_length - 1):
            full_email += (username[j]);
            if bin[j] == "1":
                full_email += "."
        full_email += (username[j + 1])
        emails.append(full_email + "mars")
    return emails

username = ("marsbros")
print(*dot_trick(username) , sep="\n",file=open("username.txt", "a"))
print(*dot_trick(username) , sep="\n")