如何在Pset 6中检查Bleep.py的Check 50?

时间:2019-03-27 09:50:48

标签: 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()

在某些情况下它可以正常打印,例如 输入:我的天哪 输出:****我的****

但其他人却不是(应该是**** ****)

输入:哎呀天哪 输出: * * (8个字母只有6 *)

1 个答案:

答案 0 :(得分:0)

是错字吗?仔细检查这条线
 censored_msg = (censored_msg + "*"*(len(token)+1) + " ")

并记住这里是for i in token: