The job:
The server receives many requests from different users or from other servers. Change the function process_query() (request handler) to support several different queries, not just one.
Add a query argument to the function process_query(). This argument will tell the query that you want to handle.
In the beginning of the function process_query() add a test variable value query:
if the value is 'How many friends do I have?' — print the answer to this question, as in the previous task;
otherwise, print '<unknown request>';
Anfisa should greet with any query.
Add a call to process_query('How many friends do I have?') in the main body of the program.
Add another call process_query('what's my name?') in the main body of the program.
Tip
Use the comparison if query == ... to define the query.
Initial code:
FRIENDS = ['Serge', 'Sonia', 'Dima', 'Alina', 'Egor']
friends_count = 0
def print_friends_count(friends_count):
if friends_count == 1:
print('you Have 1 friend')
elif 2 <= friends_count <= 4:
print('you Have' + str(friends_count) + 'friend')
elif friends_count >= 5:
print('you Have' + str(friends_count) + 'friends')
def process_query():
# move in the function process_query() here's the code:
print("hi, I'm Naomi!")
count = len(FRIENDS)
print_friends_count(count)
Naughty solution:
FRIENDS = ['Serge', 'Sonia', 'Dima', 'Alina', 'Egor']
count = len(FRIENDS)
def process_query(query):
print('hi, I'm Naomi!')
if query == 'How many friends do I have?':
def print_friends_count(count):
if count == 1:
print('you Got',count,'friend')
elif 2 <= count <= 4:
print('you Got',count,'friend')
elif count >= 5:
print('you Got',count,'friends')
print_friends_count(count)
else:
print('<unknown request>')
process_query('How many friends do I have?')
process_query('what's my name?')
The result meets the issues of the task:
Hi, I'm Naomi!
You have 5 friends
Hi, I'm Naomi!
<unknown request>
However, the bot does not accept the answer, writes "the Function of print_friends_count() does not exist. Do not remove it."
Can't understand why :(
And another question:
Function print_friends_count(count) is executed by a team print_friends_count(count). If you can, please explain why the call to the function process_query(query) is unacceptable in this program. If you call it after the ELSE statement, team process_query(query), then the cyclical output, depending on the designated function call (the padding +/- 4 spaces on the next line after the ELSE):
Hi, I'm Naomi!
You have 5 friends
Hi, I'm Naomi!
You have 5 friends
...
either
Hi, I'm Naomi!
You have 5 friends
Hi, I'm Naomi!
unknown request
Hi, I'm Naomi!
unknown request
...
You mean mark Lutz “Programming Python”? Tried to read, but the book difficult for me: a lot of theory without practice, then I don't use simply forgotten :( - philip51 commented on April 19th 20 at 12:11