+ 1

During code practice I m getting something wrong please help me by telling what's wrong in the code..

You are walking from your house to a pond that is down your street. How many blocks over will you have to walk until you get to the pond? Task: Evaluate how many blocks you will have to walk if you are given a representation of your street where H represents your house, P represents the pond, and every B represents a block in between the two. Input Format: A string of letters representing your house, the pond, and blocks on your street. Output Format: An integer value that represents the number of blocks between your house and the pond. Sample Input: BBHBBBBPBBB Sample Output: 4 ## Code string = input() index_of_H = string.index("H") index_of_P = string.index("P") blocks = index_of_P - index_of_H - 1 print(blocks) https://code.sololearn.com/cl5MiI32jQA3/?ref=app https://code.sololearn.com/cl5MiI32jQA3/?ref=app

12th Jul 2021, 7:50 AM
Bitu Kumar Sha
Bitu Kumar Sha - avatar
3 odpowiedzi
+ 3
Negative numbers may be the problem. Try blocks = abs(index_of_P - index_of_H) - 1
12th Jul 2021, 8:00 AM
David Ashton
David Ashton - avatar
+ 1
Thank you guys...I got it
12th Jul 2021, 8:05 AM
Bitu Kumar Sha
Bitu Kumar Sha - avatar
0
Your code looks okay but it won't work if a user enters lowercase strings. Convert all user inputs to uppercase.
12th Jul 2021, 8:01 AM
David Akhihiero
David Akhihiero - avatar