Frage

this is not a programming question but a question about the IDLE. Is it possible to change the comment block key from '#' to something else?

here is the part that is not going to work:

    array = []
    y = array.append(str(words2)) <-- another part of the program
    Hash = y.count(#) <-- part that won't work
    print("There are", Hash, "#'s")
War es hilfreich?

Lösung

No, that isn't specific to IDLE that is part of the language.

EDIT: I'm pretty sure you want to use

y.count('#') # note the quotes

Remember one of the strengths of Python is portability. Writing a program that would only work with your custom version of the interpreter would be removing the strengths of the language.

As a rule of thumb anytime you find yourself thinking that solution is to rewrite part of the language you might be heading in the wrong direction.

You need to call count on the string not the list:

array = []
    y = array.append(str(words2)) <-- another part of the program
    Hash = y[0].count('#')  # note the quotes and calling count on an element of the list not the whole list 
    print("There are", Hash, "#'s")

with output:

>>> l = []
>>> l.append('#$%^&###%$^^')
>>> l
['#$%^&###%$^^']
>>> l.count('#')
0
>>> l[0].count('#')
4

count is looking for an exact match and '#$%^&###%$^^' != '#'. You can use it on a list like so:

>>> l =[]
>>> l.append('#')
>>> l.append('?')
>>> l.append('#')
>>> l.append('<')
>>> l.count('#')
2
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top