我需要在函数add()
中为'地址簿'添加名称和地址,然后将其写入文件。这似乎工作正常,但是,它继续循环输入名称和地址没有任何意义。我需要它在添加联系人后返回main()
。
address={}
def add():
name=input('Enter the full name of the contact you would like to add:')
n_address=input('Enter the contacts address:')
if name not in address:
address[n_address]=name
writeFile()
else:
print('That name has already been entered.')
return address
def find():
name=input('Enter the name of the contact you would like the address for:')
if name in address:
n_address=address.get(name)
print(n_address)
else:
no=input('That name is not in the address book yet, would you like to add it? y/n:')
if no != 'n':
add()
def a_print():
if ('AddressBook.txt'):
read_file=open('AddressBook.txt','r')
mylist=read_file.readlines()
for line in mylist:
if line != '\n':
n_address, name = line.split(':')
name = name.rstrip()
address[n_address]=name
return address
print(address)
def readFile():
pass
def writeFile():
write_file=open('AddressBook.txt','w')
for (k,v) in address.items():
string=k+':'+v+'\n'
write_file.write(string)
write_file.close()
def main():
print(' Personal Address Book')
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
while choice != '0':
if choice == '1':
add()
elif choice == '2':
find()
elif choice == '3':
a_print()
main()
答案 0 :(得分:0)
你需要以某种方式取消选择。选择从不重置,你陷入了while循环。将提示逻辑放在循环中,它应该可以解决您的问题:
choice = 1 # This will get reset, anything but 0 is fine.
while choice != '0':
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
if choice == '1':
add()
elif choice == '2':
find()
elif choice == '3':
a_print()
答案 1 :(得分:0)
您的选择不在while循环中。选择是在循环之前进行的,然后进行。一旦进入循环,你就没有机制要求另一个选择,所以选择总是保持为添加。在循环结束之前,你需要一个新的选择:
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
while choice != '0':
if choice == '1':
add()
elif choice == '2':
find()
elif choice == '3':
a_print()
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
答案 2 :(得分:0)
将菜单放在while循环中
def main():
print(' Personal Address Book')
while True:
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
if choice == '0':
break
elif choice == '1':
add()
elif choice == '2':
find()
elif choice == '3':
a_print()
答案 3 :(得分:0)
您应该将菜单移到while choice != '0':
循环内。
choice = '1'
while choice != '0':
print('0 - Quit\n'
'1 - Add a New Contact\n'
'2 - Find Contacts Address\n'
'3 - Print Entire Collection\n')
choice=input('\n Choose a number corrosponding to the option above.\n')
if choice == '1':
add()
elif choice == '2':
find()
elif choice == '3':
a_print()
这样就不会让用户首先选择循环,并会提示他们选择其他选项。