I did ask this before on a different account but i lost the account and didnt see most comment or answers people gave so i have asked on this older account of mine
Im new to python and programming in general so i am not understanding what i should be doing to get the output that is expected. I think i got the read_file function correct but im not sure.
I have been looking at this for a while and im no closing to understanding how exactly to do this part.
The goal here is to display profiles from a text file but in a specific way.
the rules are:
Your solutions MAY make use of the following:
• Built-in functions int(), input(), print(), range(), open(), close(), len() and str().
• Concatenation (+) operator to create/build new strings.
• The list_name.append(item) method to update/create lists.
• Access the individual elements in a string with an index (one element only). i.e. string_name[index].
• Access the individual elements in a list with an index (one element only). i.e. list_name[index].
• Profile objects and methods (as appropriate).
• The list_function.py module (that you wrote in part A of this assignment). You may like to make use of some of the functions defined in the list_function.py module for this part of the assignment (as appropriate). Not all will be suitable or appropriate.
Your solutions MUST NOT use:
• Built-in functions (other than the int(), input(), print(), range(), open(), close() len() and str() functions).
• Slice expressions to select a range of elements from a string or list. i.e. name[start:end].
• String or list methods (i.e. other than those mentioned in the 'MAY make use' of section above.
• Global variables as described in week 8 lecture.
• The use break, return or continue statements (or any other technique to break out of loops) in your solution – doing so will result in a significant mark deduction.
here are some descriptions i have been given:
o display_summary(profile_list)
This function will take the list of profile objects as a parameter and will output the contents of the list to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'. You must use a loop in your solution.
o read_file(filename, profile_list)
This function takes a file name and reads the contents of that file into the profile_list (list) passed as a parameter into the function. The function returns the list of profile objects. You must use a loop in your solution. You may use String and/or List methods in this function only. You may find the String methods split() and strip() useful here.
the expected output is:
Please enter choice [summary|add|remove|search|update|quit]: summary
==============================================================================
Profile Summary
==============================================================================
------------------------------------------------------------------------------
Fox Mulder (m | fox@findthetruth.com)
- The truth is out there!
- Friends (1):
Tony Stark
------------------------------------------------------------------------------
Tony Stark (m | tony@ironman.com)
- Saving the world is hard work - no time for friends.
- No friends yet...
------------------------------------------------------------------------------
Phil Dunphy (m | phil@dunphy.com)
- wtf? = why the face?
- Friends (2):
Robbie Gray
Fox Mulder
------------------------------------------------------------------------------
John Mayer (m | john@guitar.com)
- Waiting on the world to change!
- Friends (2):
Katy Perry
David Guetta
------------------------------------------------------------------------------
Katy Perry (f | katy@perry.com)
- Waiting on John to change.
- Friends (3):
John Mayer
David Guetta
Jimmy Fallon
------------------------------------------------------------------------------
David Guetta (m | dguetta@willworkwithanyone.org)
- Will collaborate with anyone who has a heartbeat.
- Friends (5):
Katy Perry
John Mayer
Tony Stark
Fox Mulder
Robbie Gray
------------------------------------------------------------------------------
Jimmy Fallon (m | jimmy@tonightshow.com)
- I wish I was as good as Letterman, thank goodness he's retiring.
- Friends (2):
Robbie Gray
Tony Stark
------------------------------------------------------------------------------
Robbie Gray (m | robbie@football.com)
- Training hard... can we win? Yes we Ken!
- Friends (4):
Jimmy Fallon
Fox Mulder
John Mayer
Tony Stark
------------------------------------------------------------------------------
==============================================================================
The text is being read from a text file that looks like this:
Fox Mulder fox@findthetruth.com m
The truth is out there!
1
tony@ironman.com
Tony Stark tony@ironman.com m
Saving the world is hard work - no time for friends.
0
Phil Dunphy phil@dunphy.com m
wtf? = why the face?
2
robbie@football.com
fox@findthetruth.com
John Mayer john@guitar.com m
Waiting on the world to change!
2
katy@perry.com
dguetta@willworkwithanyone.org
Katy Perry katy@perry.com f
Waiting on John to change.
3
john@guitar.com
dguetta@willworkwithanyone.org
jimmy@tonightshow.com
David Guetta dguetta@willworkwithanyone.org m
Will collaborate with anyone who has a heartbeat.
5
katy@perry.com
john@guitar.com
tony@ironman.com
fox@findthetruth.com
robbie@football.com
Jimmy Fallon jimmy@tonightshow.com m
I wish I was as good as Letterman, thank goodness he's retiring.
2
robbie@football.com
tony@ironman.com
Robbie Gray robbie@football.com m
Training hard... can we win? Yes we Ken!
4
jimmy@tonightshow.com
fox@findthetruth.com
john@guitar.com
tony@ironman.com
I have made this so far:
import profile
def get_menu_choice():
list_choice = ['summary', 'add', 'remove', 'search', 'update', 'quit']
#User inputs an option
choice = input(str('\nPlease enter choice [summary|add|remove|search|update|quit]: '))
#Start a loop if they enter an invalid input
while choice not in list_choice:
print('\nNot a valid command - please try again.')
choice = input(str('\nPlease enter choice [summary|add|remove|search|update|quit]: '))
#end of loop here
return choice
# Function read_file() - place your own comments here... : )
def read_file(filename, profile_list):
profile_list = []
filename = open("profiles.txt", "r")
for line in filename:
stripped_line = line.strip()
profile_list = stripped_line.split()
filename.append(profile_list)
filename.close()
print(profile_list)
# Function display_summary() - place your own comments here... : )
def display_summary(profile_list):
print('========================================')
print('Profile Summary')
print('========================================')
display_details()
active = 'y'
while active == 'y':
choice = get_menu_choice()
if choice == 'summary':
display_summary()
elif choice == 'quit':
active = 'n'
the import profile is referring to this file:
class Profile:
# The __init__ method initializes the data attributes of the Profile class
def __init__(self, given_name='', family_name='', email='', gender='', status=''):
self.__given_name = given_name
self.__family_name = family_name
self.__email = email
self.__gender = gender
self.__status = status
self.__number_friends = 0
self.__friends_list = []
def set_given_name(self, name):
self.__given_name = name
def get_given_name(self):
return self.__given_name
def set_family_name(self, name):
self.__family_name = name
def get_family_name(self):
return self.__family_name
def set_email(self, email):
self.__email = email
def get_email(self):
return self.__email
def set_gender(self, gender):
self.__gender = gender
def get_gender(self):
return self.__gender
def set_status(self, status):
self.__status = status
def get_status(self):
return self.__status
def set_number_friends(self, no_friends):
self.__number_friends = no_friends
def get_number_friends(self):
return self.__number_friends
def set_friends_list(self, friends_list):
self.set_number_friends(len(friends_list))
self.__friends_list = friends_list
def get_friends_list(self):
return self.__friends_list
# The __str__ method returns a string representation of the object
def __str__(self):
string = self.__given_name + ' ' + self.__family_name + ' ' + self.__email + ' ' + self.__gender + '\n'
string += self.__status + '\n'
string += str(self.__number_friends) + '\n'
for friend_email in self.get_friends_list():
string += friend_email + '\n'
return string
# The method add_friend adds an email address to the friends_list only if the email doesn't already exist.
# No duplicate entries allowed. The method returns True if successful and False otherwise.
def add_friend(self, email):
# Check to see whether email already exists in the friends list
if self.is_friend(email) == True:
return False;
# Otherwise, okay to add friend and increment number of friends count
self.__friends_list.append(email)
self.__number_friends += 1
return True
# The method remove_friend removes an email address from the friends_list (if found).
# Method returns True if successful and False otherwise.
def remove_friend(self, email):
# Check to see whether email exists in the friends list
if self.is_friend(email) == False:
return False;
# Otherwise, okay to remove friend and decrement number of friends count
self.__friends_list.remove(email)
self.__number_friends -= 1
return True
# The method is_friend determines whether the email passed in as a parameter
# exists in the friends_list, i.e. they are friends.
# If the email is found in the friends_list, the method will return True.
# If the email is not found, the function returns False.
def is_friend(self, email):
found = False
for email_address in self.__friends_list:
if email == email_address:
found = True
return found
# The __eq__ method allows for a test for equality (is equal to) on email address i.e. == operator.
def __eq__(self, email):
if self.__email == email:
return True
elif self.__email != email:
return False
return NotImplemented
Im so confused by this. Would anyone be able to explain it or give any pointers.
Ive tried to make this question as easy to understand as i could.
Thanks.