2011-01-27 85 views
1

实现不带参数的函数标点符号(),从用户输入一个字符串,并按从左到右的顺序打印出现在字符串中的所有标点符号。实现函数

def punctuation(): 

    a = raw_input("Please enter a string:") 

    check = ['!', ',', '.', ':', '?', ';'] 
    for p in check: 
     if p in a: 
      a.remove(p) 

我不断收到的raw_input即没有定义闲置错误标志?那么我也不太熟悉split()应该做什么?温柔的家伙,我是一个蟒蛇n00b。

+2

这样的措辞听起来像是一个家庭作业问题。如果是这样,请申请'家庭作业'标签和/或提及它。如果不是,请原谅我的误解。 – Phrogz 2011-01-27 02:23:06

+1

我没有在你的代码中看到一个`split()`,但是这里的文档就在这里:http://docs.python.org/library/stdtypes.html#str.split – miku 2011-01-27 02:23:19

+0

wjat split function ?? – Jason 2011-01-27 02:24:52

回答

1

代替你可能想这样做,至于你的逻辑而言相反的方式。

  1. 维护标点符号列表(或字符串)。 (检查标准库模块字符串是否有任何帮助)
  2. 检查输入字符串的每个字符,如果它存在于上面的列表中,请将其打印出来。

貌似string模块可以ofcourse一定的帮助:

>>> import string 
>>> string.punctuation 
'!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~' 

所以,你会写你的程序沿线的东西:

for each_character in mystr: 
    if each_character in string.punctuation: 

呀,你可以在使用raw_input() python2得到你喜欢的字符串。对于python3,你应该只使用input()

1

这听起来像你正在使用Python 3 raw_input()已经与input()