【问题标题】:Problem with my character counter in python我在 python 中的字符计数器有问题
【发布时间】: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,然后粘贴进去。

标签: python count character


【解决方案1】:

在您提到粘贴多字符串输入之后,我意识到了问题(以及您的潜在解决方案)。 input 只读取 cmets 中提到的单行,但看起来您需要读取多行输入。

您可以使用下面的辅助函数multi_line_input 来做到这一点。当用户输入空字符串(空白输入)时,它将中断。然后在你的代码中你可以像往常一样获取这个字符串的长度,它应该可以工作。

def multi_line_input(prompt: str = '') -> str:
    """Reads multiple line from user input. An empty line breaks the loop."""
    lines = []
    while True:
        line = input(prompt)
        if line:
            # Note: added strip() here to strip out spaces at end,
            # such as 'hello '
            lines.append(line.rstrip())
        else:
            break
    return '\n'.join(lines)

【讨论】:

  • 这个函数怎么称呼?
  • input
  • 您必须在代码上方或其他模块中定义该函数,否则您将收到 NameError。另一个将所有逻辑包装在 main 函数中的修复方法,然后您可以在它下面定义这个并仍然从 main 调用它。
猜你喜欢
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2017-11-03
  • 2019-03-01
  • 1970-01-01
  • 2022-06-15
  • 2020-02-29
  • 1970-01-01
相关资源
最近更新 更多