def add_friend(): # Create a new Spy object spy_friend = Spy() spy_friend.set_spy_name(input("\nPlease enter your friend's name: ")) spy_friend.set_spy_salutation( input("Is your friend a Mr., a Ms. or a Mrs.? ")) spy_friend.set_spy_age(int(input("How old is your friend? "))) spy_friend.set_spy_rating(float(input("What is your friend's rating? "))) # Perform some checks on the details entered if (len(spy_friend.get_spy_name()) > 0 and 12 < spy_friend.get_spy_age() < 50 and spy_friend.get_spy_rating() >= spy.get_spy_rating()): # Add the friend to the list of friends spy.add_friend(spy_friend) # Also, write the friend's details to friends.csv with open("friends.csv", "a") as friends_data: writer = csv.writer(friends_data) writer.writerow([ spy_friend.get_spy_name(), spy_friend.get_spy_salutation(), spy_friend.get_spy_age(), spy_friend.get_spy_rating() ]) else: print( "Sorry! Some of the details you provided for your friend might have been invalid." ) return len(spy.get_spy_friends())
def load_friends(): with open("friends.csv", "r") as friends_data: reader = csv.reader(friends_data) for row in reader: spy_friend = Spy() spy_friend.set_spy_name(row[0]) spy_friend.set_spy_salutation(row[1]) spy_friend.set_spy_age(int(row[2])) spy_friend.set_spy_rating(float(row[3])) spy.get_spy_friends().append(spy_friend)
# Print the entire chat history between the user and the friend in the desired coloured format for chat in chat_history: sender = colored(chat.get_sender() + ": ", "red") time = colored(" [" + chat.get_time() + "]", "blue") message = chat.get_message() print(sender + message + time) # Ask for user details upon starting the application while True: spy_name = input("Please tell me your name: ") if len(spy_name) > 0: spy.set_spy_name(spy_name) spy.set_spy_salutation(input("Should I call you Mr. or Mrs. : ")) spy.set_spy_age(int(input("What is your age?"))) spy.set_spy_rating(float(input("What is your spy rating?"))) spy.set_spy_is_online(True) break else: print("Please enter a valid spy name.") # Optional objective 4: When the application starts it should automatically load the friends from friends.csv load_friends() # Optional objective 3: When the application starts it should automatically load the chats from chats.csv load_chats() # Start the chat start_chat()