# Variables names must start with a letter or an underscore.
condition = False
#Lets check whether the first character is either a letter or an underscore.
if name[0].isalpha():
condition = True
elif name[0] == '_':
condition = True
else:
condition = False
pass
# Set condition to False just so it's more readable for me.
#Lets check the rest of the elements starting after 1 to see whether
#they don't contain a non-letter, number or
#underscore
for e in name[1:]:
if e.isnumeric() or e.isalpha() or e == '_':
condition = True
else:
condition = False
return condition
所以基本上它返回True或False,无论提供的变量名是否有效。有人能给我一个暗示我做错的事吗?
答案 0 :(得分:1)
您的问题在循环for e in name[1:]:
内。每次循环播放时,您都会将condition
设置为True
或False
,,无论原始值是什么。您可能希望在condition
变为假时立即退出循环。