import re
social = '1234'
phone = '5678'
user = int(input("Please tell me your last four digit of your social: "))
user1 = int(input("your last digit of your phone number: "))
info_checker = re.search("^12.*34$", social)
info_checker1 = re.search("^56.*78$", phone)
if info_checker and info_checker1:
print("Great you're all set")
else:
print("Sorry we couldn't find this info")
每当我输入用户的数据时,即使是对的或错的,它也只打印出if语句?
你必须使用正确的变量在 search()
- user
而不是 social
和 user1
而不是 phone
info_checker = re.search("^12.*34$", user)
info_checker1 = re.search("^56.*78$", user1)
EDIT: 而且不要把用户的字符串转换成整数。
import re
user = input("Please tell me your last four digit of your social: ")
user1 = input("your last digit of your phone number: ")
info_checker = re.search("^12.*34$", user)
info_checker1 = re.search("^56.*78$", user1)
if info_checker and info_checker1:
print("Great you're all set")
else:
print("Sorry we couldn't find this info")