2016-10-08 3 views
0

連絡先を保存して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が生成されます。

関連する問題