开发者

Implement a function

开发者 https://www.devze.com 2023-02-07 03:24 出处:网络
Implement a function punctuation() that takes no parameters, inputs a string from the user, and prints all of the punctuation characters appearing in the string, in order from left to right.

Implement a function punctuation() that takes no parameters, inputs a string from the user, and prints all of the punctuation characters appearing in the string, in order from left to right.

def punctuation():

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

    check = ['!', ',', '.', ':', '?', ';']
    for p in check:
        if p in开发者_如何学JAVA a:
            a.remove(p)

I keep getting an error sign on IDLE that raw_input isn't defined? Then also, I'm not too familiar what the split() is supposed to do? Be gentle guys, I'm a python n00b.


You might want to do it the reverse way as far as your logic is concerned.

  1. Maintain a list (or string of) punctuation characters. (Check if the standard library module string has any help here)
  2. Go through each character of your input string and if it is present in the list above, print it.

Looks like string module can ofcourse be of help here:

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

So, you will write your program something along the lines:

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

Yeah, you can use raw_input() in python2 to get the string like you are getting. For python3, you should just use input()


It sounds like you are using Python 3. raw_input() has been replaced with input()

0

精彩评论

暂无评论...
验证码 换一张
取 消