Boredless Tourist

Boredless Tourist

So, I’ve almost finished the assignment, but I want to perfect the interests_string a bit more, by trying to make it so that after the last attraction it’ll use a period instead of a comma. But I don’t know how so I need some help. Many thanks.

Hello @iori-ri237!!
In the future could you please format your code, it makes it much easier to read.
You can see how here

How to format code?

Regarding your question, I would get rid of your if statement on line 94, since you know you are going to have a period at the end there is no reason to check for a condition.

Instead unindent line 95, so that it line up with the for loop on line 91, and will be read as soon as the for loop finishes.

The below code should give you some idea of what I mean.

for i in range(7):

print i + '.'

Hopefully this helps :slight_smile:

1 Like

It helps quite a bit, thank you!

Edited: I fixed my code, is this what you meant?

def get_attractions_for_traveler(traveler):
  traveler_destination = traveler[1]
  traveler_interests = traveler[2]
  traveler_attractions = find_attractions(traveler_destination, traveler_interests)
  interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination + ": " 
  for i in range(len(traveler_attractions)):
    interests_string += attraction
    interests_string += ", "
  interests_string += "."
  return interests_string

i think the string will have a last comma before the period though? I want to eliminate that last comma.

Edited (again): I forgot about how the range stops right before the parameter index. I think it’ll work now.
Thanks for helping me!

1 Like

Yes!! :grinning:
Sorry I missed that extra comma…

To remove the last comma you can use the .rstrip() operator.
This operator will remove the trailing characters in a string, that are specified by its argument:

string = 'The cat ate the fish?'

string = string.rstrip('?')

string += '!'

The first line printed will be:
‘The cat ate the fish?’

The second line will be:
‘The cat ate the fish’

The third line will be:
‘The cat ate the fish!’

An extra note for your circumstance is that since you have a space after your comma you will have to add that space in .rstrips() argument.

string = "cat, dog, sheep, pig, "
print(string.rstring(', ')

Note the space after the comma…

1 Like

Wow, this is really helpful, thank you! Almost didn’t notice the space.

1 Like

Your certainly welcome!! Glad to help!! :grinning:

1 Like