2012-12-17 145 views
3

我是新来的网站,到Python,所以我希望我给所有需要的信息。我已经搜索了这个问题,但没有一个解决方案似乎适用于我。返回/打印量或错误消息

我试图创建一个函数读取一些文本,然后返回它包含数字量。例如:

“这是2012”,应返回“文本有4位”

如果没有数字,它应该返回不同的消息,如:

“这是星期一”,应该返回“文本中不包含数字”

所以我写了这一点:

def CountNumbers(txt): 
    sum = 0 

    for n in txt: 
     if n.isdigit() == False: 
      print ("Text does not contain digits") 

     else: 
      sum += 1 


    print("Text has", sum, "digit(s)") 


txt = str(input("Write some text: ")) 
(CountNumbers(txt)) 

的功能似乎是OK然而,打印出错误,例如:

Write some text: 65 
Text has 2 digit(s) #this is ok, but... 

当我只输入文本:

Write some text: sd 
Text does not contain digits 
Text does not contain digits 
Text does not contain digits 
Text has 0 digit(s) 

当我输入文本和数字(但文本在前):

Write some text: sd 564 
Text does not contain digits 
Text does not contain digits 
Text does not contain digits 
Text has 3 digit(s) 

我知道我的错误在于块,(我想想),但我还没有想出办法,因为当我使用return时,它在阅读文本完成之前停止。我已经尝试了大约20多种不同的东西,请帮助我!

谢谢!

P.S.我需要这样做,作为一个.py而不只是在IDLE(Python Shell)窗口中,这就是为什么我要写这样的块。

+2

+1简短的自我包含的代码和样本输入/输出。多个样本,甚至!爱它。 – Kevin

+2

请记住,根据Python的命名约定,您的函数应该被命名为'count_numbers()'而不是'CountNumbers'。 – pemistahl

+0

另外,你不应该命名一个变量'sum',因为这是一个内置函数的名字。 – abarnert

回答

2

我只是解决了你的代码的问题:

def CountNumbers(txt): 
    sum = 0 

    for n in txt: 
     if n.isdigit(): 
      sum += 1 

    if sum: 
     print("Text has", sum, "digit(s)") 
    else: 
     print ("Text does not contain digits") 


CountNumbers(input("Write some text: ")) 
+0

您的回答非常有帮助,谢谢! ...我想知道,为什么你不写 如果sum> 0: 只是如果总和...他们都工作,但我想知道为什么。我认为只是“如果总结:”甚至不是一个条件! haha – JotaSolano

+1

@ user1911059 - 如果您将一个数字评估为布尔值,如果它为零则为“False”,否则为“True”。因此,在'if sum:'这样的语句中,如果'sum'大于零,即至少有一个数字,则它为'真'。 – Blair

4

的问题是要打印“文本中不包含数字”的消息一遇到非数字字符的时间。尝试打印语句移动到循环结束后,当你知道有多少个字符有:

def CountNumbers(txt): 
    sum = 0 

    for n in txt: 
     if n.isdigit(): 
      sum += 1 

    if sum > 0: 
     print("Text has", sum, "digit(s)") 
    else: 
     print ("Text does not contain digits") 

txt = str(input("Write some text: ")) 
(CountNumbers(txt)) 

编辑:

  • 作为的评论指出:一对夫妇更朝着好的Python代码点你的问题,按照惯例,功能/ Python方法都在用下划线,即小写命名,count_numbersCountNumbers
  • 迂腐模式:您计算的字符串,而不是数字的数字,所以它应该被命名为count_digits清晰。
  • 有一个内置Python函数调用sumshadow此功能通过具有相同名称的变量被认为是不好的做法。我会将您的sum变量重命名为count
  • 没有必要包围在括号中的函数调用(它不会使它看起来很糟糕恕我直言,做任何伤害,除了)。

有了这些变化,你的代码就变成了:

def count_digits(txt): 
    count = 0 

    for n in txt: 
     if n.isdigit(): 
      count += 1 

    if count > 0: 
     print("Text has", count, "digit(s)") 
    else: 
     print ("Text does not contain digits") 

txt = str(input("Write some text: ")) 
count_digits(txt) 
1

早期的回报是相当好的,可以大大简化代码:

def CountNumbers(txt): 
    for n in txt: 
     if not n.isdigit(): 
      print ("Text does not contain digits") 
      return 

    print("Text has", len(txt), "digit(s)") 

txt = str(input("Write some text: ")) 
(CountNumbers(txt)) 

此功能,一旦返回一个非数字字符出现,或打印输入字符串的长度(由而不是数字组成)。

你也可以写一个“一代表达”,如:

def CountNumbers(txt): 
    if all(char.isdigit() for char in txt): 
     print("Text has", len(txt), "digit(s)") 
    else: 
     print ("Text does not contain digits") 

txt = str(input("Write some text: ")) 
(CountNumbers(txt)) 

现在,Python的功能更典型的返回值,让它调用代码的行为。该版本返回字符串并让程序的“main”部分打印结果。它还重命名功能全部小写(Python中通常使用大写的名字班):

def countnumbers(txt): 
    if all(char.isdigit() for char in txt): 
     return "Text has %d digit(s)" % len(txt) 
    else: 
     return "Text does not contain digits" 

txt = str(input("Write some text: ")) 
print(countnumbers(txt)) 

嗯,不是太寒酸!但是Python也有像俏皮的条件表达式:

def countnumbers(txt): 
    return ("Text has %d digit(s)" % len(txt) 
     if all(char.isdigit() for char in txt) 
     else "Text does not contain digits") 

txt = str(input("Write some text: ")) 
print(countnumbers(txt)) 

最后,也许你会希望是为适当的模块,这样其他的代码可以使用它。总结你的程序是这样的互动部分,如果你喜欢命令行脚本运行它,它才会执行,但不如果您导入它作为一个模块:

def countnumbers(txt): 
    return ("Text has %d digit(s)" % len(txt) 
     if all(char.isdigit() for char in txt) 
     else "Text does not contain digits") 

if __name__ == '__main__': 
    txt = str(input("Write some text: ")) 
    print(countnumbers(txt)) 

如果我写一个作为生产系统的一部分,这与它的外观非常接近。请注意,逻辑与您的第一次尝试完全相同。最大的区别是,这个版本让Python可以完成大部分工作。

+0

哇这真的很有趣,我对这种写作仍然不熟悉,但是这确实扩展了我的理解力。谢谢 ! – JotaSolano

+0

检查下面的注释:'all(char.isdigit()for char in txt)'不能按预期工作 - 应该是all([char.isdigit()for char in txt])'。除此之外,原来的问题表明: '“它是2012”,应该返回“文本有4位数字”。在这种情况下,不会出现短路的错误答案? – subnivean

0

原始的稍微pythonic重做。就个人而言,我觉得“列表理解”形成更容易一些阅读(除非它是嵌套):

def count_digits(txt): 
    count = len([c for c in txt if c.isdigit()]) 

    if count > 0: 
     print ("Text has", count, "digit(s)") 
    else: 
     print ("Text does not contain digits") 
+0

在Python 2.4及更高版本中,您可以将其写为'count = len(c for c in txt if if c.isdigit())'。这是一个“生成器表达式”。 –

+0

我早些时候曾尝试过,得到类型为'generator'的对象没有len()''。在IPython中运行2.7.3。 – subnivean

+0

忽略我。我的下午咖啡还没有踢。 :-D –