【发布时间】:2021-11-15 12:23:30
【问题描述】:
我正在尝试用 python 编写一个程序来计算文本中的字符。当我输入没有像Hello world! 这样的新行的文本并说有12个字符但是当我输入类似的东西时它可以工作
Hello
world!
然后它只计算第一行并说有 5 个字符。 )=
我的代码:
import time
print("Please answer Y or N to the following questions:")
whatTo1Do = input("Would you like to include spaces in the character count? " )
whatTo2Do = input("Would you like to include commas in the character count? " )
whatTo3Do = input("Would you like to include apostrophes in the character count? " )
whatTo4Do = input("Would you like to include maths symbols (+, <, ÷, etc.) in the character count? " )
whatTo5Do = input("Would you like to include brackets in the character count? " )
whatTo6Do = input("Would you like to include other characters (~, `, \, %, &, etc.) in the character count? " )
yas = input("Your settings are displayed above. Are they correct? ")
if yas == "Y":
pass
elif yas == "N":
print("Please press OK to restart.")
time.sleep(1)
exit()
text = input("Enter your text here to count the characters based on your settings above: ")
if whatTo1Do == "N" or whatTo1Do == "n" or whatTo1Do == "no":
text.replace(" ", "")
if whatTo2Do == "N" or whatTo2Do == "n" or whatTo2Do == "no":
text.replace(",", "")
if whatTo3Do == "N" or whatTo3Do == "n" or whatTo3Do == "no":
text.replace("'", "")
text.replace('"', "")
if whatTo4Do == "N" or whatTo3Do == "n" or whatTo3Do == "no":
text.replace("×", "")
text.replace("÷", "")
text.replace("=", "")
text.replace("-", "")
text.replace("+", "")
text.replace(">", "")
text.replace("<", "")
text.replace("^", "")
text.replace("*", "")
if whatTo5Do == "N" or whatTo3Do == "n" or whatTo3Do == "no":
text.replace("[", "")
text.replace("]", "")
text.replace("(", "")
text.replace(")", "")
text.replace("{", "")
text.replace("}", "")
if whatTo6Do == "N" or whatTo3Do == "n" or whatTo3Do == "no":
text.replace("~", "")
text.replace("`", "")
text.replace("@", "")
text.replace("#", "")
text.replace("$", "")
text.replace("%", "")
text.replace("&", "")
text.replace("\ ", "")
text.replace(" \ ", "")
text.replace("|", "")
text.replace("/", "")
print("There are", len(text), "characters in your text. (=")
我该怎么做才能让它同时计算两条线?
【问题讨论】:
-
你的密码是什么?
-
将该代码粘贴到您的问题中。
-
评论太长了。
-
我把
hello\nworld写在其他地方,比如google docs,然后粘贴进去。