-1

Q. We're using lists to record people who attended our party and what order they arrived in. For example, the following list represents a party with 7 guests, in which Adela showed up first and Ford was the last to arrive:

party_attendees = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford'] A guest is considered 'fashionably late' if they arrived after at least half of the party's guests. However, they must not be the very last guest (that's taking it too far). In the above example, Mona and Gilbert are the only guests who were fashionably late.

Complete the function below which takes a list of party attendees as well as a person, and tells us whether that person is fashionably late.

My Ans:

def fashionably_late(arrivals, name):
"""Given an ordered list of arrivals to the party and a name, return whether the guest with that
name was fashionably late.(This is a hint by the website.)
"""
name_index=arrivals.index(name) #This line of code is working fine.
final_index_of_list=arrivals.index((len(arrivals)-1)
print(final_index_of_list)
if final_index_of_list%2==0:
    return False if name_index<=(final_index_of_list/2) or name_index==final_index_of_list else True
else final_index_of_list%2>0:
    return False if name_index<=(final_index_of_list/2+1) or name_index==final_index_of_list else True

Error Message: 
File "<ipython-input-16-d4bcd37e23f2>", line 7
    print(final_index_of_list)
        ^
SyntaxError: invalid syntax
  • I have no idea what's wrong with print and when I remove the print line the error changes to ":" at the end of the first "if" statement. – Belal Noor Sep 01 '20 at 14:28

1 Answers1

0

You forged bracket in line 6.

Instead:

final_index_of_list=arrivals.index((len(arrivals)-1)

should be:

final_index_of_list=arrivals.index((len(arrivals)-1))

UPD:

I find next some mistakes:

  • Incorrect function indentation. See more here
  • Instead else final_index_of_list%2>0: should be elif final_index_of_list%2>0:. You can read about python conditional statements here

So, correct function is:

def fashionably_late(arrivals, name):
    """Given an ordered list of arrivals to the party and a name, return whether the guest with that
    name was fashionably late.(This is a hint by the website.)
    """
    name_index=arrivals.index(name) #This line of code is working fine.
    final_index_of_list=arrivals.index((len(arrivals)-1))
    print(final_index_of_list)
    if final_index_of_list%2==0:
        return False if name_index<=(final_index_of_list/2) or name_index==final_index_of_list else True
    elif final_index_of_list%2>0:
        return False if name_index<=(final_index_of_list/2+1) or name_index==final_index_of_list else True
MeenLike
  • 9
  • 4
  • Even after adding the bracket it throws error before (final_index_ of_list) in "else final_index_of_list%2>0:" line. Thanks for the help. I don't wanna bother someone like you again. Any advice for me for not happening this again? – Belal Noor Sep 01 '20 at 14:52
  • @BelalNoor I update answer. In the future, you can use python linters to check whether the code is written incorrectly:) – MeenLike Sep 02 '20 at 06:46
  • Thanks! @MeenLike – Belal Noor Sep 03 '20 at 13:45