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.
My Github
How to post code using bbtags


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.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,947 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  I'm getting a String index out of range error debian77 7 2,277 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  For loop index out of bounds armitron121 2 2,617 Feb-08-2022, 04:23 PM
Last Post: armitron121
  string index out of range jade_kim 4 2,563 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,138 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 4,955 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,149 May-17-2020, 05:56 PM
Last Post: shockwave
  Python 2 to 3 dict sorting joshuaprocious 2 53,532 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,393 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  Sort a dict in dict cherry_cherry 4 62,323 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