Correct variable names consist only of English letters, digits and underscores and they can‘t start with a digit.
Check if the given string is a correct variable name.
Example
- For
name = "var_1__Int"
, the output should bevariableName(name) = true
; - For
name = "qq-q"
, the output should bevariableName(name) = false
; - For
name = "2w2"
, the output should bevariableName(name) = false
.
我的解答:
def variableName(name): dict = {‘word‘:‘qwertyuiopasdfghjklzxcvbnm‘, ‘digit‘:‘0123456789‘, ‘underline‘:‘_‘} if name[0].lower() in dict[‘word‘] or name[0] in dict[‘underline‘]: for i in name: if i.lower() in dict[‘word‘] or i in dict[‘underline‘] or i in dict[‘digit‘]: pass else: return False return True else: return False
def variableName(name): return name.isidentifier()
膜拜大佬
原文地址:https://www.cnblogs.com/YD2018/p/9470193.html
时间: 2024-10-15 01:32:51