def add_friend(): new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("Please add your friend's name: ") if new_friend.name.isalpha( ) == True: # isalpha() is function here used to check whether an entry from user is string or not. new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("What is their Age?") if new_friend.age.isdigit(): new_friend.age = int(new_friend.age) new_friend.rating = raw_input("what is their Spy rating?") if new_friend.rating.isdigit(): new_friend.rating = float(new_friend.rating) if len( new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'Friend Added!' else: print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' else: print "Rating is not feasible." else: print "Age must be of integer type. " else: print "please add a valid spy name!" return len(friends)
def add_spy_friend(): # Spy is a class new_spy_friend = Spy('', '', 0, 0.0) new_spy_friend.name = raw_input( colored("Please add your friend's name:", 'red')) new_spy_friend.salutation = raw_input( colored("Are they Mr. or Ms.?: ", 'red')) new_spy_friend.age = raw_input(colored("Age?", 'red')) new_spy_friend.age = int(new_spy_friend.age) new_spy_friend.rating = raw_input(colored("Spy rating?", 'red')) new_spy_friend.rating = float(new_spy_friend.rating) if len( new_spy_friend.name ) > 0 and new_spy_friend.age > 12 and new_spy_friend.rating >= spy.rating: friends.append(new_spy_friend) print colored('Friend Added', 'red') total_spy_friend() else: print colored( 'Sorry! Invalid entry. We can\'t add spy with the details you provided', 'red') return len(friends)
def add_friend(): new_friend = Spy('','',0,0.0) new_friend.name = raw_input("Please add your friend's name: ") #Handling edge case scenarios. if set('[~!@#$%^&*()_+{}":;\']+$ " "').intersection(new_friend.name): print "Invalid entry. " new_friend.name = raw_input("Please re enter a valid name") else: print new_friend.name new_friend.salutation = raw_input("How would you like to address them? ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("How old is your friend?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("And their rating too please?") new_friend.rating = float(new_friend.rating) #Mandatory conditions to be satisfied for adding a friend. if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print "Congratulations! You have a new friend." else: print "Uh huh! Your friend is not good enough. Try again with different details." return len(friends)
def add_friend(): #Function defined to add spy's friends alongwith the details of them new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("Enter your friend's name Spy: ") if set('[~!@#$%^&*()_+{}":;\']+$ " "').intersection(new_friend.name): print "Invalid entry. Please Enter a valid name of single word without special characters" new_friend.name = raw_input("Enter your friend's name Spy: ") else: print "Thats a valid friend name" new_friend.salutation = raw_input("MR or MS : ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("How old is your mate?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("How amazing are they?") new_friend.rating = float(new_friend.rating) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= 3.5: friends.append(new_friend) print 'You have a new Friend <3!' else: print 'Sorry! This friend isn\'t worth you. We can\'t add spy with the details you gave' return len(friends)
def add_friend(): new_friend = Spy('', '', 0, 0.0, '') new_friend.name = raw_input( "Let\'s add your friend. Enter your friends name:") new_friend.salutation = raw_input( 'What would you like us to call him/her? Mr. or Ms.?:') new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Enter Age") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Enter spy rating") new_friend.rating = float(new_friend.rating) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print colored('Congratulation! Your friend has been added!', 'blue', 'on_grey') else: print colored('OOPS! Unable to add spy with the details you provided', 'red', 'on_white') return len(friends)
def add_friend(): #add_friend function will add a new friend to the friends list. new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("please add your friends name: ") new_friend.salutation = raw_input("Are they Mr or Ms?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input('Age?') new_friend.age = int(new_friend.age) new_friend.rating = raw_input('rating?') new_friend.rating = float(new_friend.rating) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'Friend Added!' print friends else: print 'sorry!Invalid entry.We cant add spy with the detail you provided' return len(friends)
def add_friend(): # Using the class spy new_friend = Spy(" ", " ", 0, 0.0) new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") # ask for the age of the friend new_friend.age = raw_input("Age?: ") # Type casting to integer new_friend.age = int(new_friend.age) # Ask for the rating of the friend new_friend.rating = raw_input("Spy rating?: ") # Type casting to float new_friend.rating = float(new_friend.rating) # Add a friend of correct age and equal or higher rating # Valid name of strings must be entered if len(new_friend.name) > 0 and new_friend.name.isdigit() == False and 12 < new_friend.age < 50 and new_friend.salutation.isalpha() == True and new_friend.rating >= spy.rating : # After the conditions are satisfied the friend will be added friends.append(new_friend) print(colored('Friend Added!', "cyan")) else: print(colored("Sorry, the friend cannot be a spy!", "blue")) # The no of friends the spy has will be returned. return len(friends)
def add_friend(): #Initializing The Variable new_friend new_friend = Spy('', '', 0, 0.0) #Getting Name Of the Friend new_friend.name = raw_input("Please add your friend's name: ") #Getting Salutation Of the Friend new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") #Concatinating Salutation And Name new_friend.name = new_friend.salutation + " " + new_friend.name #Getting Age new_friend.age = raw_input("Age?") #Converting Age To Integer new_friend.age = int(new_friend.age) #Getting Rating new_friend.rating = raw_input("Spy rating?") #Converting Rating To FloatingPoint new_friend.rating = float(new_friend.rating) #If Friend's Name is Not Empty ,Age is above 12 and friend's rating is above or equals to spy's rating if len( new_friend.name ) > 0 and new_friend.age > 12 and new_friend.age < 50: #new_friend.rating >= spy.rating: friends.append(new_friend) #Add Friend to The Friends List print 'Friend Added!' else: #If The Required Conditions are not Met print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' return len(friends) #Return The Length of the friends list
def add_friend(): new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("Please add your friend's name: \n") if set('[special symbols":;\']+$ " "').intersection(new_friend): print "Invalid entry." else: print new_friend new_friend.salutation = raw_input( "Formal Salutation: Mr. or Ms. or Mrs. \n") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Age?\n") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Spy rating?\n") new_friend.rating = float(new_friend.rating) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'New Friend Added!\n' else: print "Sorry! Invalid entry. We can\'t add spy with the details you provided!!\n" return len(friends)
def add_friend(): #spy is a class for adding a friend new_friend = Spy('', '', 0, 0.0) #input your friends name please!!!! new_friend.name = raw_input( "Please enter your friend's name you would like to add in a spyChat: ") new_friend.salutation = raw_input("Are your friend Mr. or Ms.?: ") #printing friends name with salutation new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Please enter your Age?") #converting string to integer as age is an integer, not a string new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Please enter your Spy rating?") #converting string to float as rating is an float, not a string new_friend.rating = float(new_friend.rating) #based on the below conditions if it meets, then friends will be added else not added if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'Congratulations!!!!! Friend Added successfully!' else: print 'Sorry for that! we can\'t add your friend due to invalid entry.' #returning number of friends added return len(friends)
def add_friend(): new_friend = Spy("", "", 0, 0.0) new_friend.name = raw_input("Whats the name of your friend?") if len(new_friend.name) > 0 and new_friend.name.isspace() == False: new_friend.salutation = raw_input( "What should we call your friend: Mr. or Ms.?") if new_friend.salutation == "Mr." or new_friend.salutation == "Ms.": new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Whats the age?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Whats the rating?") new_friend.rating = float(new_friend.rating) if new_friend.age > 12 and new_friend.rating > 3.5: friends.append(new_friend) print "New friend added!!" else: print "Sorry! Can't add the friend with the information provided!!" else: print "Enter Mr. or Ms." else: print "Enter valid name!!" return len(friends)
def add_friend(): #use of dictionary to store details of a new friend newfriend = Spy('','', 0, 0.0) adding_more = True while adding_more: newfriend.name = raw_input(colored("Please add your friend's name:\n", "blue")) newfriend.salutation = raw_input(colored("What should i call him/her Mr or Miss:\n", "blue")) newfriend.age = input(colored('Enter your friend\'s age:\n', "blue")) newfriend.rating = input(colored('What\'s your friends spy rating:\n', "blue")) newfriend.rating = float(newfriend.rating) #validation of the friend if len(newfriend.name) > 0 and 12 < newfriend.age < 40 and newfriend.rating >= spy.rating and newfriend.name.isalpha()==False: friends.append(newfriend) print colored("%s %s is added to your friend list as a new spy\n", "green")%newfriend.salutation,newfriend.name #writing the friends details to file friends.csv with open('friends.csv', 'a') as friend_details: #opening the file in append mode writer = csv.writer(friend_details) writer.writerow([newfriend.name, newfriend.salutation, newfriend.age, newfriend.rating, newfriend.mode]) else: #if friend is not eligible to be a spy print colored("Sorry! Your friend do not meet our criteria to be a your spy friend\n", "red") while True: #to add more friends more = raw_input(colored("Do you want to add more spy friends (Y/N)?\n", "blue")) if more.upper() == 'N': adding_more = False break elif more.upper() == 'Y': break else: print colored("Press a valid key Y or N\n", "red") return len(friends)
def add_friend(): #function for adding a new friend to the list friends new_friend = Spy("", "", 0.0, 0) new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.age = raw_input("Age?:") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Spy rating?:") new_friend.rating = float(new_friend.rating) if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating > spy.rating:#conditions for being a spy friend friends.append(new_friend) print "Friend Added!" else: print "Sorry!your friend cant be a spy!!" return len(friends)
def add_friend(): new_friend = Spy('', '', 0, 0.0) #friend name,salutation age and rating is added to data new_friend.name = raw_input("Please add your friend's name: ") if set('[~!@#$%^&*()+{}":;\']" "').intersection(new_friend.name): print "Invalid entry. Please enter a Valid Name!" new_friend.name = raw_input("enter your friend's name=") if set('[~!@#$%^&*()+{}":;\']" "').intersection(new_friend.name): print "Invalid name! Please try again." exit() else: print "That's a valid friend name" new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Age of your friend ?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Spy rating?") new_friend.rating = float(new_friend.rating) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print "Friend Added!" else: print "Sorry!. We can't add spy with the details you provided" return len(friends)
def add_friend(): # initially setting details of friend to zero new_friend = Spy('', '', 0.0, 0) # asking the user to enter name new_friend.name = raw_input('Please tell your friend name:') # asking the user to enter salutation new_friend.salutation = raw_input('What should we call you Mr. or Ms.:') # asking the user to enter rating and converting it to float new_friend.rating = float(raw_input('Please tell your friends rating:')) # asking the user to enter age and converting it to integer new_friend.age = int(raw_input('Please tell your friends age:')) # check the standards of rating spy_rating(new_friend.rating) # checking the salutation # checks if user has right age to proceed, valid name and rating greater than user if 12 < new_friend.age < 50 and name_validation(new_friend.name) and 5 >= new_friend.rating >= spy.rating and \ valid_salutation(new_friend.salutation): # stripping off spaces before and after his name new_friend.name = new_friend.name.strip(" ") new_friend.salutation = new_friend.salutation.strip(" ") # appending the new friend to friends list friends.append(new_friend) print 'Friend added' else: print 'Sorry we can\'t enter spy with that invalid details or your age might be low' # returning the no. of friends to the calling function return len(friends)
def add_friend(): new_friend = Spy("", "", 0, 0) new_friend.name = raw_input("Enter your friend's name :\n") new_friend.salutation = raw_input("Is your friend Mr. or Ms. :\n") new_friend.age = int(raw_input("Enter your friend's age :\n")) new_friend.rating = float(raw_input("Enter your friend's rating :\n")) # Checking eligibility of new friend # The criteria that his spy_rating should be greater than mine seems to be a bit wonky :-( if len( new_friend.name ) > 0 and 12 < new_friend.age < 50 and spy.rating <= new_friend.rating <= 5.0: # Adding a new friend to my friends list friends.append(new_friend) print new_friend.salutation + " " + new_friend.name + " is now your friend" else: print "This spy is not eligible to be your friend!" # Returning the no of friends return len(friends)
def add_friend(): #creating a new default friend new_friend = Spy("", "", 0, 0.0) time.sleep(0.3) new_friend.name = raw_input( "Input your friends name: ") #asking for friend's name new_friend.name = check_name( new_friend.name) #checking the validity of the name time.sleep(0.3) new_friend.sal = raw_input( "Is your friend Mr or Ms: ") #asking foor salutation new_friend.sal = check_Sal( new_friend.sal) #checking the validity of the salutation time.sleep(0.3) new_friend.age = int( raw_input("Age of your spy friend: ")) #age input in int time.sleep(0.3) new_friend.rating = input( "Enter your spy friend's spy rating: ") #rating input in float if (check_age(new_friend.age)): #check for appropriate age friends.append( new_friend ) #if valid new friend to be appended at the end of the friends list #writing details of new friend at the end of the file using ab with open("friends.csv", "ab") as friends_data: writer = csv.writer(friends_data) writer.writerow([ new_friend.name, new_friend.sal, new_friend.age, new_friend.rating, new_friend.is0nline ]) else: time.sleep(0.3) print "Sorry you are not of appropriate age to be a Spy!" #inappropriate age return len(friends) #return length of friends list
def add_friend(): #create a list to hold friend names,friend ages and friend rating new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name #ask the user age new_friend.age = raw_input("Age?") new_friend.age = int(new_friend.age) #ask the user_rating new_friend.rating = raw_input("Spy rating?") new_friend.rating = float(new_friend.rating) #check condition if name is not empty ,age is gretaer than 12 and rating of the friend spy is greater than or equal to the user spy rating if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) #print the appropiate message print 'Friend Added!' else: print 'Sorry! Invalid rating entry. We can\'t add spy with the details you provided' #return no of friends the user has return len(friends)
def add_friend(): # using class spy new_friend = Spy(" ", " ", 0, 0.0) # ask user for name and salutation of friend new_friend.name = raw_input("Please add your friend's name: ") pattern_n = '^[a-zA-Z\s]+s' new_friend.salutation= raw_input("What do we call you Mr. or Ms.?: ") pattern_s = '^[a-zA-Z\s]+s' # concatination for full name new_friend.name = new_friend.salutation + " " + new_friend.name # ask for age of friend, int represents type casting in int new_friend.age = int(raw_input("Age? ")) pattern_a = '^[0-9]+$' #ask for rating of friend, float represents type casting in float new_friend.rating = float(raw_input("Spy rating? ")) pattern_r = '^[0-9]+\.[0-9]+$' # users input validations if len(new_friend.name) > 0 and new_friend.name.isdigit() == False and re.match(pattern_n,new_friend.name)!=None and re.match(pattern_s,new_friend.salutation)!=None and new_friend.age > 12 and new_friend.rating < 50 and re.match(pattern_a,new_friend.age)!=None and new_friend.salutation.isalpha() == True and new_friend.rating >= 0 and re.match(pattern_r,new_friend.rating)!=None: # add friend if conditions satisfies friends.append(new_friend) print (colored('Friend Added!', 'red')) else: print (colored('Sorry! Invalid entry. We can\'t add spy with the details you provided\n ', 'blue')) print (colored('Please enter the right data, don\'t make mistakes. ', 'blue')) # returning total no of friends. return len(friends)
def add_friend(): new_friend = Spy('', '', 0, 0.0) # Raw input allows to take input from user and new_friend.name is a variable where variable is stored. new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") # Variable has been updated new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Age?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Spy rating?") new_friend.rating = float(new_friend.rating) # spy friend should meet the requirements for which we apply condition using IF keyword if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: # .append adds friend to new_friend friends.append(new_friend) print 'Friend Added!' else: # \ is used before 't which is used for escaping character # Another better way of doing this is "Sorry! Invalid entry. We can\'t add spy with the details you provided" print 'Sorry! Invalid entry. We can\'t add spy with the details you just provided. .' return len(friends)
def add_friend(): # class object new friend is initialised new_friend = Spy('', '', 0, 0.0) #get details of friend and save them in class variables new_friend.name = raw_input("please add your friend's name: ") new_friend.salutation = raw_input('Mr or Ms? :') new_friend.name = new_friend.salutation + ' ' + new_friend.name new_friend.age = int(raw_input('Age?:')) new_friend.rating = float(raw_input('Rating of spy?:')) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'friend added!!' else: print 'sorry we can\'t add spy with the details you provided' return len(friends)
def add_friend(): new_friend = Spy('','',0,0.0) new_friend.name = raw_input("enter your friend's name: ") new_friend.salutation = raw_input("are they (Mr. or Ms. or Mrs.)?: ") if (new_friend.salutation == 'Mr.'): print 'he is a male' elif(new_friend.salutation == 'Ms.') or (new_friend.salutation == 'Mrs. '): print'she is a female' else: print 'please enter a valid salutation' new_friend.salutation = raw_input("are they (Mr. or Ms. or Mrs.)?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input("Age? ")) new_friend.rating = float(raw_input("Spy rating? ")) if len(new_friend.name) > 0 and new_friend.age > 15 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'Friend Added!' else: print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' return len(friends)
def add_friend(): new_friend = Spy('', '', 0, 0.0) # Asking details of new friend new_friend.name = raw_input("Please add your friend's name: ") if (new_friend.name > 0): new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") if (new_friend.salutation > 0): new_friend.name = new_friend.salutation + " " + new_friend.name else: print 'Provide proper salutation' new_friend.age = int(raw_input("Age?")) new_friend.rating = float(raw_input("Spy rating?")) if len( new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print '\n Friend Added!' else: print '\n Sorry! Invalid entry. We can\'t add spy with the details you provided' else: print '\n Provide proper name!!' # Returning length of friends list i.e number of friends return len(friends)
def add_friend(): # using class spy new_friend = Spy(" ", " ", 0, 0.0) # ask user for name and salutation of friend new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation= raw_input("What to call Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input("Age? ")) new_friend.rating = float(raw_input("Spy rating? ")) if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.age < 50 and new_friend.age : # add friend if conditions satisfies friends.append(new_friend) with open('friends.csv', 'wb') as friends_data: writer = csv.writer(friends_data) writer.writerow([new_friend.name, new_friend.salutation, new_friend.rating, new_friend.age, new_friend.is_online]) print (colored('Friend Added!', 'magenta')) else: print (colored('Sorry! Invalid entry. We can\'t add spy with the details you provided\n ', 'blue')) print (colored('The convention to follow is: \n ', 'blue')) print (colored('1. Name can be alphabets only.\n ', 'blue')) print (colored('2. Age can be digits only.\n ', 'blue')) # returning total no of friends. return len(friends)
def add_friend(): new_friend = Spy(" ", " ", 0, 0.0) new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("What to call Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input("Age? ")) new_friend.rating = float(raw_input("Spy rating? ")) if len(new_friend.name) > 0 and new_friend.name.isdigit( ) == False and re.match(pattern_n, new_friend.name) != None and re.match( pattern_s, new_friend.salutation ) != None and new_friend.age > 12 and new_friend.rating < 50 and re.match( pattern_a, new_friend.age) != None and new_friend.salutation.isalpha( ) == True and new_friend.rating >= 0 and re.match( pattern_r, new_friend.rating) != None: friends.append(new_friend) print(colored('Friend Added!', 'blue')) else: print( colored( 'Sorry! Invalid entry. We can\'t add spy with the details you provided\n ', 'blue')) print(colored('Please enter the right data, don\'t make mistakes.', 'blue')) return len(friends)
def add_freind(): new_friend = Spy("", 0, 0.0, True) new_friend.name = raw_input("What is the name of friend ? ") new_friend.salutation = raw_input( "What should we call you ? (Mr. or Ms.) ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = input("What is the age of friend ? ") new_friend.rating = input("What is your rating ? ") if len(new_friend.name ) >= 3 and 50 >= new_friend.age and new_friend.rating >= spy.rating: friends.append(new_friend) print "friend is added" with open('friends.csv', 'a') as friends_data: writer = csv.writer(friends_data) writer.writerow([ new_friend.name, new_friend.age, new_friend.rating, new_friend.is_online ]) else: print "Freind cannot be added" return len(friends)
def add_friend(): # new dictonary for every new friend addded new_friend = Spy('', '', 0, 0.0) # friends Info new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input(new_friend.name + "\'s Age? ")) new_friend.rating = float(raw_input(new_friend.name + "\'s Smart rating? ")) if len(new_friend.name ) > 0 and new_friend.age > 12 and new_friend.rating >= 1: try: with open("friends_data.csv", 'ab') as friends_data: write = csv.writer(friends_data) write.writerow([ new_friend.name, new_friend.salutation, new_friend.age, new_friend.rating ]) except: print 'File not available' friends.append(new_friend) print '\nNew Friend Name " %s " Age " %d " of Rating " %.2f " Added into Friend List.' % ( new_friend.name, new_friend.age, new_friend.rating) else: print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' # return length of frineds list return len(friends)
def add_friend(): # The function should ask the user for the name, age and rating of their spy friend new_friend = Spy('','',0,0.0) # The function converts the data as appropriate new_friend.name = raw_input("Please add your friend's name: ") new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = raw_input("Age?") new_friend.age = int(new_friend.age) new_friend.rating = raw_input("Spy rating?") new_friend.rating = float(new_friend.rating) # The function should add the friend to various lists only if name is not empty, age is greater than 12 and rating of the friend spy is greater than or equal to the user spy rating. if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) print 'Friend Added!' # If any of the above conditions are not met it displays an appropirate message else: print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' # The function should return and print the number of friends the user has return len(friends)
def add_friend(): new_friend = Spy('','',0,0.0) new_friend.name = raw_input("Please add your friend's name: ") try: assert any([char not in string.ascii_letters for char in new_friend.name]) is False print "valid name" new_friend.salutation = raw_input("Are they Mr. or Ms.?: ") new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input("Age?")) new_friend.rating = float(raw_input("Spy rating?")) if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating >= 4: friends.append(new_friend) print 'Friend Added!' else: print 'Sorry! Invalid entry. We can\'t add spy with the details you provided' return len(friends) except AssertionError: print "invalid name"
def add_friend(): new_friend = Spy(" ", " ", 0, 0.0) new_friend.name = raw_input("Please add your friend's name?") # here len()>0 func checks that user should not press enter without giving any name # isspace() func does not allow the user to enter blank spaces in place of name if len(new_friend.name) > 0 and new_friend.name.isspace() == False: new_friend.salutation = raw_input("Are they Mr. or Ms.?") # to check that user does not enter anything else except Mr. or Ms. if new_friend.salutation == "Mr." or new_friend.salutation == "Ms.": new_friend.name = new_friend.salutation + " " + new_friend.name new_friend.age = int(raw_input("Age?")) new_friend.rating = float(raw_input("Spy rating?")) if new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) #adding new friends to old list print "New friend added!" else: print "Sorry! We can't add a spy with the details you provided.Please enter valid age or valid rating.." else: print "Please enter valid option.Choose either Mr. or Ms." else: print "Please enter valid spy name!!" return len(friends) # returns no of friends in friends list
def add_friend(): new_friend = Spy('', '', 0, 0.0) new_friend.name = raw_input("please add your friends name : ") new_friend.salutation = raw_input("are they mr or miss : ") new_friend.age = int(raw_input("age: ")) new_friend.rating = float(raw_input("rating: ")) if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating: friends.append(new_friend) else: print("sorry! invalid entry. we can't add a spy with the details you provided. please try again later. ") return len(friends)