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
#2
Please use proper tags when posting.

Here is a working example

#! /usr/bin/env python3

roman = {
    'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000
}

def convert(rn):
    converted = []
    for val in rn:
        if val in roman.keys():
            converted.append(roman[val])
    return sum(converted)
print(convert('XVI'))
Output:
16
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#3
Gonna go ahead and add one to convert to roman numerals. This one took a while to figure out. Using the same dict as above.
def to_roman(num):
    keys = []
    values = []
    for key in roman.keys():
        keys.append(key)

    for value in roman.values():
        values.append(value)
    keys.reverse()
    values.reverse()

    rom = ''
    i = 0
    while num > 0:
        for _ in range(num // values[i]):
            rom += keys[i]
            num -= values[i]
        i += 1
    return rom

print(to_roman(326))
Output:
CCCXXVI
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string index out of range jade_kim 4 552 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 2,150 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 738 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  TypeError: __repr__ returned non-string (type dict) shockwave 0 1,132 May-17-2020, 05:56 PM
Last Post: shockwave
  Python 2 to 3 dict sorting joshuaprocious 2 794 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 1,561 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  Sort a dict in dict cherry_cherry 4 1,031 Apr-08-2020, 12:25 PM
Last Post: perfringo
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 940 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  String index out of range - help please DudleyDiccle 7 1,176 Mar-27-2020, 12:10 AM
Last Post: DudleyDiccle
  Change string into Dict Robin_at_Cantelli 2 1,575 Mar-05-2020, 10:37 AM
Last Post: vishalhule

Forum Jump:

User Panel Messages

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