Keith Mort thedude3610 Keith Mort thedude3610 - 12 days ago 4x
Python Question

Error with useing .split() with user input in python

So I am trying to make a basic address book in python that can save contacts and load them using pickle, but for some reason I get an error when I try to put in a contact
My code is:

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+'))
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])
elif choice == 3:
print('Invalid input.')

The error says:

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

Any help would be greatly appreciated, thanks.


I think you're looking for raw_input. Unlike in Python 3, in Python 2.x, input parses and evaluates the string as a Python expression.

From the input docs:

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised.