Как выполнить проверку 50 для Bleep.py в Pset 6?

#python-3.x #cs50

#python-3.x #cs50

Вопрос:

Количество напечатанных символов «*» колеблется в зависимости от длины введенной строки

 def main():
    # check command line argument
    if len(argv) != 2:
        print("Usage: python bleep.py dictionary")
        exit(1)

    else:
        ban = set()

        # Load txt file
        with open(argv[1], "r") as f:
            for line in f:
                # strip the space and add to set
                ban.add(line.strip())

        # prompt user input
        input = get_string("What message would you like to censor?n")

        # Split input into word tokens
        token = input.split()
        censored_msg = ""

        for i in token:
            if i.lower() in ban:
                censored_msg = (censored_msg   "*"*(len(token) 1)   " ")
            else:
                censored_msg  = i   " "

        # print censored message
        print(censored_msg.strip())

if __name__ == "__main__":
    main()
  

В некоторых случаях он печатается нормально, например
Ввод: черт возьми
Вывод: **** мой ****

Но не так в других (должно быть ********)

Ввод: черт возьми, Вывод: * * (всего 6 * для 8 букв)

Ответ №1:

Это опечатка? Проверьте эту строку очень тщательно
censored_msg = (censored_msg "*"*(len(token) 1) " ")

и помните, что здесь есть for i in token: