i was trying to solve security code for the code coach but it gives me error in (if i > 0 and i <= length-1:) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

i was trying to solve security code for the code coach but it gives me error in (if i > 0 and i <= length-1:)

but it gives me error in (if i > 0 and i <= length-1:) if floor TxGx$ floor_map = input("Enter the floor map ") floor_map = [floor_map.replace("x", "")] #print(floor_map) length = len(floor_map[0]) i = 0 p = "" while i in range(length): a = floor_map[0][i] if a == "

quot; : if i > 0 and i <= length-1: if floor_map[0][i-1] == "T" or floor_map[0][i+1] == "T" : p = "ALARM" i = length else: p = "" elif i == 0 : if floor_map[0][i+1] == "T" : p = "ALARM" i = length else: p = "" elif i == length : if floor_map[0][i-1] == "T" : p = "ALARM" i = length else: p = "" i += 1 if p != "ALARM": p = "nothing" print(p) Ajith

1st Sep 2020, 6:36 AM
Smiley[Offline]
8 Answers
+ 1
the code right below it. [i+1] could lead into index out of bound error. since i<=length-1. [i+1] can get equal to length. but since list is start with 0. the last index must me at length-1. so if i+1 is equal to length, its out of bounds
1st Sep 2020, 7:04 AM
Rei
Rei - avatar
+ 1
Md. Nasif-ur-Rahman Rimon sir I was just testing the code by adding some extra print but I would be greatful if you could just explain me why is it giving me error in the 11 statement i.e. if i > 0 and i <= length-1: I didn't wanted the solution for this problem
1st Sep 2020, 6:48 AM
Smiley[Offline]
1st Sep 2020, 6:51 AM
Smiley[Offline]
+ 1
Thanks
1st Sep 2020, 7:06 AM
Smiley[Offline]
+ 1
This is not the correct way to do the problem! You have to find a better shorter way to do it! According to PEP8 => there's only 1 best method to do a problem! Why to complicate things? for eg: x = input().replace("x","") if "$T" in x or "T
quot; in x: print("ALARM") else: print("quiet")
1st Sep 2020, 7:26 AM
Namit Jain
Namit Jain - avatar
0
and is not an operator in java if you want to check that two conditions are true use '&&'.
1st Sep 2020, 6:46 AM
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž - avatar
0
Sorr my bad
1st Sep 2020, 6:51 AM
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž - avatar
0
Wrong way ๐Ÿ˜…
1st Sep 2020, 6:53 AM
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž
โ€ŽูŠูˆุณู ุณุนุฏโ€Ž - avatar