-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_friend.py
61 lines (54 loc) · 2.37 KB
/
add_friend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from spy_details import *
import csv
def add_friend():
# using class user in spy_details
new_friend = Spy(" ", " ", 0, 0.0)
# ask user for name
new_friend.name = raw_input("Please add your friend's name: ")
#-------------------------------------------------------------------------------------------------------------------
# user name validation.
if len(new_friend.name) > 0:
if len(new_friend.name) > 20:
print("Your name length is big.")
else:
print("Name should be not empty or length is less then 20 char.")
return add_friend()
new_friend.salutation = raw_input("What to call Mr. or Ms.?: ")
#-------------------------------------------------------------------------------------------------------------------
# user salutation validation
if len(new_friend.salutation) > 0:
if len(new_friend.salutation) > 5:
print("Your salutation is too big.")
else:
print("Salutation empty or check length")
return add_friend()
# concatination for full name
new_friend.name = new_friend.salutation + " " + new_friend.name
#-------------------------------------------------------------------------------------------------------------------
# ask for age of friend
new_friend.age = int(raw_input("Age: "))
if 12 < new_friend.age < 50:
True
else:
print("Age should be in between 12 to 50")
return add_friend()
#-------------------------------------------------------------------------------------------------------------------
#ask for rating of friend, using float
new_friend.rating = float(raw_input("Spy rating? "))
if new_friend.rating > 0.0:
True
else:
print("Ratting should be more than 0.0")
return add_friend()
new_friend=Spy(name=new_friend.name, salutation=new_friend.salutation, age=new_friend.age, rating=new_friend.rating)
# add friend if all conditions check
# friends.append(new_friend)
# add friend if all conditions check
friends.append(new_friend)
print('Friend Added!')
with open("friends.csv", "a") as friends_data:
writer = csv.writer(friends_data)
writer.writerow(
[new_friend.name, new_friend.salutation, new_friend.age, new_friend.rating, new_friend.is_online])
# check total no of friends in a list.
return len(friends)