2016-10-08 54 views
0

所以我想在python中製作一個基本的地址簿,它可以保存聯繫人並使用pickle加載它們,但由於某種原因,當我嘗試將錯誤在接觸 我的代碼是:在python中使用.split()與用戶輸入的錯誤

import pickle 
class Contact: 
    def __init__(self, firstname, lastname, phonenumber, adress): 
     self.firstname = firstname 
     self.lastname = lastname 
     self.phonenumber = phonenumber 
     self.adress = adress 
print('Welcome to your adress book, would you like to:\n1. Add a contact\n2. Look at a contact\n3. Close the program') 
while True: 
    choice = int(input()) 
    if choice == 1: 
     print('Type in their information, with the format\nfirstname lastname phonenumber adress') 
    info = input().split() 
    pickle.dump(info, open(info[0].lower() + '.pkl' , 'w+')) 
    continue 
elif choice == 2: 
    print('What is their first name?') 
    fn = input().lower() 
    x = pickle.load(open(fn + '.pkl', 'rb')) 
    print(x[0] + x[1] + x[2] + x[3]) 
    continue 
elif choice == 3: 
    break 
else: 
    print('Invalid input.') 
    continue 

錯誤說:

Traceback (most recent call last): 
    File "C:\Users\keith\Desktop\adress_book.py", line 13, in <module> 
    info = input().split() 
    File "<string>", line 1 
    Bob Smith 123456789 123street 
      ^
SyntaxError: invalid syntax 

任何幫助將不勝感激,謝謝。

回答

0

我認爲您正在尋找raw_input。與Python 3不同,在Python 2.x中,input將字符串解析並評估爲Python表達式。

input文檔:

此功能不會趕上用戶錯誤。如果輸入語法無效,將會產生一個SyntaxError