2017-10-19 85 views
0

对于第三个elif,我试图让它检测'Numb'中是否有任何特殊字符,并且是否有重复的while循环。如何查找输入中是否有任何特殊字符

我一直在使用re.match与进口重新尝试,但它似乎并不奏效

Numb = input('Enter the number you want to find the factorial of: ') 

if Numb.isalpha() == True:  
    print ('You can\'t find the factorial of a letter stupid! Try a digit 
next time') 
elif int(Numb) <0: 
    print ('You cannot find the factorial of a negative number') 
elif Numb >=0 and Numb.isdigit() == True: 
    print ('::::::::::::::::' ':'*len(Numb)) 
    print ('you have chosen', Numb) 
    print ('::::::::::::::::' ':'*len(Numb)) 
    Con +=1 
elif re.match("^[a-zA-Z0-9]*$", Numb): 
    print ('Do not enter any special characters. e.g. \' \' or \'.\'') 

else: 
    print ('Please entar an integer that is 0 or above') 

任何帮助将不胜感激,我仍然很新的这

+0

你的输入是什么? – anupsabraham

+0

发生了什么,你想要发生什么? – mrCarnivore

+0

1.使用小写变量名; 2.不要使用'condition == True' – Dabiuteef

回答

1

您需要使用re.search()而不是re.match() ! 你的正则表达式应该检查不是一个特殊字符的一个或多个(+)作为

re.search("[^a-zA-Z0-9]+", Numb) 

检查一个或多个会检查任何比赛与或多个特殊字符这是不希望的一在你的问题!

样品IO:

>>> re.search("[^a-zA-Z0-9]+","2343") #false 
>>> re.search("[^a-zA-Z0-9]+","2343$") #true 
<_sre.SRE_Match object at 0x7fdcbaed07e8> 
>>> re.search("[^a-zA-Z0-9]+","2343$3534") #true 
<_sre.SRE_Match object at 0x7fdcbaed08b8> 
>>> re.search("[^a-zA-Z0-9]+","2345$$$43") #true 
<_sre.SRE_Match object at 0x7fdcbaed07e8> 
>>> re.search("[^a-zA-Z0-9]+","34dsf") #false 
>>> re.search("[^a-zA-Z0-9]+","fdsf") #false 
-1
import string 
    Numb = input('Enter the number you want to find the factorial of: ') 
    if len(set(string.punctuation).intersection(Numb)) > 0: 
     print('Do not enter any special characters.') 

这应该工作,我做我的脚本相反的(我只是与数据库和密码验证发挥各地,但也有参数的密码,如特殊字符,帽子等#

+0

这不是重点,这只是鳍d如果有特殊字符!在这里批评我的代码是愚蠢的。这没有完整的代码,他没有要求那只愚蠢的鹅。他有检查这个数字是否没有任何特殊字符的代码。你也回答了,那么这只是为了与我竞争?我是新来的,不知道人们做了什么,我认为我们只是在这里帮助别人,这就是我打算做的。 –

相关问题