Python Forum
String index out of bounds ( Python : Dict )
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String index out of bounds ( Python : Dict )
#1
Hello All,

I am trying to convert Roman number to Integer (III-->3) and to do that I need to compare characters one by one in a dictionary. How do I not get this error?

If this was a string I can do where S[i]=s[i+1], Tried to do same in the dictionary but it is throwing me an error.


def prit(num,count):
i=0
while(i<len(num)):
if(len(num)==1):
count = count + dict1[num[0]]
if(len(num)==0):
print(count)
exit()
else:
if(dict1[num[i]]>=dict1[num[i+1]]):
count = count + dict1[num[i]]
i=i+1
else:
count = dict1[num[i+1]]-dict1[num[i]]+count
i=i+2

return count
num="LVIII"
count=0
dict1 = {"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000

}
count=prit(num,count)
print(count)
Reply


Messages In This Thread
String index out of bounds ( Python : Dict ) - by kommu - Jun-25-2020, 03:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,109 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  I'm getting a String index out of range error debian77 7 2,354 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  For loop index out of bounds armitron121 2 2,680 Feb-08-2022, 04:23 PM
Last Post: armitron121
  string index out of range jade_kim 4 2,638 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 1 12,268 Jan-07-2021, 06:19 PM
Last Post: deanhystad
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,021 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,196 May-17-2020, 05:56 PM
Last Post: shockwave
  Python 2 to 3 dict sorting joshuaprocious 2 59,213 May-14-2020, 03:28 PM
Last Post: joshuaprocious
  IndexError: index 0 is out of bounds for axis 0 with size 0 error tmhsa 5 4,474 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  Sort a dict in dict cherry_cherry 4 75,029 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020