def add_friend(): frnd = Spy('', '', 0, 0.0) frnd.name = raw_input("Write your friend's name") frnd.sal = raw_input("Mr. or Ms.") frnd.name = frnd.sal + " " + frnd.name frnd.age = input("Enter Age") frnd.rating = input("Enter rating of your friend") frnd.online = True if len(frnd.name ) > 2 and 50 >= frnd.age >= 12 and frnd.rating >= spy.rating: friends.append(frnd) with open('friends.csv', 'a') as friends_data: writer = csv.writer(friends_data) writer.writerow([frnd.name, frnd.rating, frnd.age, frnd.online]) else: print("Friend with these values cannot be added ") return len(friends)
def add_friend(): # adding a blank friend new_friend = Spy('', '', 0, 0.0) print "Fill the details of your friend." # getting the name of friend and also checking validation new_friend.name = get_name() # if valid name succesfully received getting futher details and converting in respective data types if new_friend.name: new_friend.age = int(raw_input("Whats your friend's age?")) # checking for validation of age if new_friend.age > 12: new_friend.rating = float(raw_input("Whats your friend's spy rating?")) if new_friend.rating >= spy.rating: # changing the online status of friend new_friend.online = True # adding and saving the new friend to friend list friends.append(new_friend) print "Friend Added" else: print "Your friend's rating is less than yours and hence we cannot add your friend!" # if the age of spy is not fit else: print "Your friend's age is not fit to be a spy." # if the name is not valid else: print "Sorry!!We cannot add your friend as you provided an invalid name." # returning number of friends return len(friends)
print 'Welome ' + spy.name + '.Glad to see you again!' # now we add more information by making some variables # we use int(raw_input()) to convert the string into integer directly spy.age = int(raw_input('Enter your age:')) # we can use and to compare two things if spy.age > 12 and spy.age < 50: spy.rating = float(raw_input('Enter your spy rating:')) # for cases we use if elif(use in making of many cases) and finally else if spy.rating > 4.5: print 'You are awesome spy' elif spy.rating > 3.5 and spy.rating <= 4.5: print 'You are good spy ' elif spy.rating >= 2.5 and spy.rating <= 3.5: print 'You can do better' else: print 'We can use somebody to help in office' # Making spy online after getting details spy.online = True else: print 'Sorry for inconvenience , but your age is not valid' start_chat(spy) else: print 'Please enter your name and try again.'