我怎样才能将输入字符限制为字母
How can i restrict input characters to just letters
我不太了解python,我现在正在研究逆向工程,为了一个培训计划crackme.exe我在python上找到了这段代码。请告诉我如何检查数字的输入字符 - 只需要字母,必须排除数字或输出“输入错误”
def keygen(name):
word = 0
for letter in name:
word += ord(letter)
return word ^ 0x5678 ^ 0x1234
import sys
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Name")
sys.exit(1)
else:
print(keygen(sys.argv[1].upper()))
sys.argv
sys.exit(1)
您可以遍历输入字符串的每个字符并检查是否是字母表中的字母。
我在下面创建了一个名为 is_valid
的函数,它应该可以解决您的问题。如果要更改哪些字符有效,可以通过编辑 alphabet
变量来实现。
alphabet = "abcdefghijklmnopqrstuvwxyz"
def is_valid(text):
for char in text:
# char.lower() makes sure the text is lowercase, otherwise "Hello" would not be valid because capital H wasn't in the alphabet.
if char.lower() not in alphabet:
return False
else: # We've finished iterating, which means all characters were in the alphabet.
return True
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Name")
sys.exit(1)
else:
if is_valid(sys.argv[1]): # Yay! Its valid so we can run our function
print(keygen(sys.argv[1].upper()))
else:
sys.exit(1) # Its not valid so we should exit with Code 1
我不太了解python,我现在正在研究逆向工程,为了一个培训计划crackme.exe我在python上找到了这段代码。请告诉我如何检查数字的输入字符 - 只需要字母,必须排除数字或输出“输入错误”
def keygen(name):
word = 0
for letter in name:
word += ord(letter)
return word ^ 0x5678 ^ 0x1234
import sys
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Name")
sys.exit(1)
else:
print(keygen(sys.argv[1].upper()))
sys.argv
sys.exit(1)
您可以遍历输入字符串的每个字符并检查是否是字母表中的字母。
我在下面创建了一个名为 is_valid
的函数,它应该可以解决您的问题。如果要更改哪些字符有效,可以通过编辑 alphabet
变量来实现。
alphabet = "abcdefghijklmnopqrstuvwxyz"
def is_valid(text):
for char in text:
# char.lower() makes sure the text is lowercase, otherwise "Hello" would not be valid because capital H wasn't in the alphabet.
if char.lower() not in alphabet:
return False
else: # We've finished iterating, which means all characters were in the alphabet.
return True
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Name")
sys.exit(1)
else:
if is_valid(sys.argv[1]): # Yay! Its valid so we can run our function
print(keygen(sys.argv[1].upper()))
else:
sys.exit(1) # Its not valid so we should exit with Code 1