+ 1
Word matcher,
I once tried a program that, for a given string, it started printing random characters till they match the one I wrote. Like: Input: Hello Print: goej Print: ucjw Print: Hfuo Print: Hcfo Print: Helgo Print: Hello My question is: Do I have to make a list with every character to try to match them, or is there another way to do it? I would like to avoid: chars = ["a", "b", "c"...]
2 ответов
+ 7
I know a way!
use ascii code!
97 - 122 is the lowercase alphabet
and 65 - 90 is uppercase
use char(number here) to convert a ascii to a regular letter, and ord(character here) to convert to ascii.
I'm sure you could come up with a for loop that uses it
+ 3
Do you want also to avoid:
chars = "abcd...yzAB...XYZ"
?
Because these are not ugly code themselve, just visually more aggressive in the first case :P
...and the second case is still accessible as an array ^^:
for c in chars:
if c in mystring:
# do your stuff here when find char
else:
# do your stuff here when you don't