如何通过 Pset 6 中 Bleep.py 的 Check 50?
How to past Check 50 for Bleep.py in Pset 6?
打印的“*”个数根据输入字符串的长度而变化
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 *)
是打错了吗?仔细检查这一行
censored_msg = (censored_msg + "*"*(len(token)+1) + " ")
记住这里是什么for i in token:
打印的“*”个数根据输入字符串的长度而变化
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 *)
是打错了吗?仔细检查这一行
censored_msg = (censored_msg + "*"*(len(token)+1) + " ")
记住这里是什么for i in token: