我有一个简单的Python问题,我的大脑停滞不前。此代码段有效。但是,当我用phoneNumber替换“ 258 494-3929”时,出现以下错误:
# Compare phone number phone_pattern = '^\d{3} ?\d{3}-\d{4}$' # phoneNumber = str(input("Please enter a phone number: ")) if re.search(phone_pattern, "258 494-3929"): print "Pattern matches" else: print "Pattern doesn't match!"
Pattern does not match Please enter a phone number: 258 494-3929 Traceback (most recent call last): File "pattern_match.py", line 16, in <module> phoneNumber = str(input("Please enter a phone number: ")) File "<string>", line 1 258 494-3929 ^ SyntaxError: invalid syntax C:\Users\Developer\Documents\PythonDemo>
顺便说一句,我没有import re和尝试使用rstrip的情况下\n
import re
rstrip
\n
我还能缺少什么?
您应该使用raw_input而不是input,而不必调用str,因为此函数本身返回一个字符串:
raw_input
input
str
phoneNumber = raw_input("Please enter a phone number: ")