我想编辑给定文件address.txt中的手机号码,但是当我输入了错误的手机号码时,如果我在address.txt文件中有三个条目,则此代码会多次打印“手机号码未找到”,那么它将打印三次“未找到手机号码“。我想打印一次我的其他声明帮助吗?
def editContact():
obj2 = open("address.txt","r")
output = []
number = raw_input("Enter 1 for edit email id and Enter 2 for edit phone number : ")
if(number=="1"):
old_number=raw_input("Enter old phone number : ")
for line in obj2.readlines():
if old_number in line:
print line
new_number=raw_input("Enter new phone number : ")
obj2 = open("address.txt","r")
aa=obj2.read()
s = re.sub(old_number, new_number, aa)
obj1 = open("address.txt","w")
obj1.writelines(s)
print "Number Edit sucessfully"
else:
print "Mobile number not found"
else:
print "Incorrect choice"
输出
Mobile number not found
Mobile number not found
Mobile number not found
所需的输出:
Mobile number not found
答案 0 :(得分:1)
根据我的理解:您的文件有3个条目(3个电话号码),如果找不到输入条目,您只想打印一次。
import re
def editContact():
obj2 = open("address.txt", "r")
output = []
number = raw_input("Enter 1 for edit email id and Enter 2 for edit phone number : ")
if (number == "1"):
old_number = raw_input("Enter old phone number : ")
for line in obj2.readlines():
if old_number in line:
print line
new_number = raw_input("Enter new phone number : ")
obj2 = open("address.txt", "r")
aa = obj2.read()
s = re.sub(old_number, new_number, aa)
obj1 = open("address.txt", "w")
obj1.writelines(s)
print "Number Edit sucessfully"
break
else:
print "EOF Reached, number not found!"
else:
print "Incorrect choice"
editContact()
输出是:
Enter 1 for edit email id and Enter 2 for edit phone number : 1
Enter old phone number : 123
EOF Reached, number not found!
答案 1 :(得分:0)
我建议使用一个布尔变量。在这种情况下,present
是在函数的第6行引入的布尔变量。
def editContact():
obj2 = open("address.txt","r")
output = []
number = raw_input("Enter 1 for edit email id and Enter 2 for edit phone number : ")
if(number=="1"):
old_number=raw_input("Enter old phone number : ")
present = False # this will check if number is present or not
for line in obj2.readlines():
if old_number in line:
present = True # since number is present make it True
print line
new_number=raw_input("Enter new phone number : ")
obj2 = open("address.txt","r")
aa=obj2.read()
s = re.sub(old_number, new_number, aa)
obj1 = open("address.txt","w")
obj1.writelines(s)
print "Number Edit sucessfully"
#After every line in text file is parsed check its value
if present == False:
print "Mobile number not found"
else:
print "Incorrect choice"
答案 2 :(得分:0)
def editContact():
obj2 = open("address.txt","r")
output = []
number = raw_input("Enter 1 for edit email id and Enter 2 for edit phone number : ")
if(number=="1"):
old_number=raw_input("Enter old phone number : ")
for line in obj2.readlines():
if old_number in line:
print line
new_number=raw_input("Enter new phone number : ")
obj2 = open("address.txt","r")
aa=obj2.read()
s = re.sub(old_number, new_number, aa)
obj1 = open("address.txt","w")
obj1.writelines(s)
print "Number Edit sucessfully"
break
else:
print "Mobile number not found"
else:
print "Incorrect choice"
将else
语句移出循环将解决问题。
此外,break
语句中的if
是必要的,否则它也会重复。