#Kraig Andrews #Roman calculator #Proj04.py s1 = raw_input('Enter the first roman numeral:') s1 = s1.upper() s2 = raw_input('Enter the second roman numeral:') s2 = s2.upper() t1 = 0 t2 = 0 c1 = 0 c2 = 0 c3 = 0 while c1 <= len(s1): i = s1[c1] if i == 'I': t1 += 1 if c1+1 < len(s1): if s1[c1+1] == 'X': t1 -= 2 if s1[c1+1] == 'V': t1 -= 2 elif i == 'V': t1 += 5 elif i == 'X': t1 += 10 if c1+1 < len(s1): if s1[c1+1] == 'L': t1 -= 20 if s1[c1+1] == 'C': t1 -= 20 elif i == 'L': t1 += 50 elif i == 'C': t1 += 100 c1 += 1 if c1 == len(s1): break print 'Value of %s: %s' %(s1,t1) while c2 <= len(s2): j = s2[c2] if j == 'I': t2 += 1 if c2+1 < len(s2): if s2[c2+1] == 'X': t2 -= 2 if s2[c2+1] == 'V': t2 -= 2 elif j == 'V': t2 += 5 elif j == 'X': t2 += 10 if c2+1 < len(s2): if s2[c2+1] == 'L': t2 -= 20 if s2[c2+1] == 'C': t2 -= 20 elif j == 'L': t2 += 50 elif j == 'C': t2 += 100 c2 += 1 if c2 == len(s2): break t3 = t1 + t2 print 'Value of %s: %s' %(s2,t2) print 'Vale of %s + %s = %s' %(t1,t2,t3) s3 = '' while t3 >= 0: if t3 >= 100: s3 += 'C' t3 -= 100 elif t3 >= 10: if t3 == 40: s3 += 'XL' t3 -= 40 elif t3 == 90: s3 += 'XC' t3 -= 90 elif t3 == 10: s3 += 'X' t3 -= 10 elif t3 == 50: s3 += 'L' t3 -= 50 elif t3 >= 1: if t3 == 1: s3 += 'I' t3 -= 1 elif t3 == 4: s3 += 'IV' t3 -= 4 elif t3 == 5: s3 += 'V' t3 -= 5 elif t3 == 9: s3 += 'IX' t3 -= 9 if t3 == 0: break print 'The sum of %s + %s = %s' %(s1,s2,s3)

# Why is this in an infinite loop?

Page 1 of 1## 4 Replies - 944 Views - Last Post: 09 October 2011 - 06:53 PM

### #1

# Why is this in an infinite loop?

Posted 09 October 2011 - 04:40 PM

ok so this is becoming frustrating, I have been working on this for a while and everything should be right (obviously it is not), but the program cannot get out of the loop and I cant see why.

##
**Replies To:** Why is this in an infinite loop?

### #2

## Re: Why is this in an infinite loop?

Posted 09 October 2011 - 05:11 PM

What if t3 is the value 2, 3, 6, 7, 8? That loop will never end because it will go into the t3 >= 1 elif, but never enter one of the other if/elifs. Thus will never end. Check the values of c1, c2 and t3 and always make sure you account for all values.

To show you... do inputs "III" and "V" which is 8. Endless loop. Do inputs "III" and "VII" which is 10, successfully ends loop.

To show you... do inputs "III" and "V" which is 8. Endless loop. Do inputs "III" and "VII" which is 10, successfully ends loop.

This post has been edited by **Martyr2**: 09 October 2011 - 05:22 PM

### #3

## Re: Why is this in an infinite loop?

Posted 09 October 2011 - 05:22 PM

ok I understand what you are saying, but i am unsure how to fix this, here is what i tried to do to fix it:

#Kraig Andrews #Roman calculator #Proj04.py s1 = raw_input('Enter the first roman numeral:') s1 = s1.upper() s2 = raw_input('Enter the second roman numeral:') s2 = s2.upper() t1 = 0 t2 = 0 c1 = 0 c2 = 0 c3 = 0 while c1 <= len(s1): i = s1[c1] if i == 'I': t1 += 1 if c1+1 < len(s1): if s1[c1+1] == 'X': t1 -= 2 if s1[c1+1] == 'V': t1 -= 2 elif i == 'V': t1 += 5 elif i == 'X': t1 += 10 if c1+1 < len(s1): if s1[c1+1] == 'L': t1 -= 20 if s1[c1+1] == 'C': t1 -= 20 elif i == 'L': t1 += 50 elif i == 'C': t1 += 100 c1 += 1 if c1 == len(s1): break print 'Value of %s: %s' %(s1,t1) while c2 <= len(s2): j = s2[c2] if j == 'I': t2 += 1 if c2+1 < len(s2): if s2[c2+1] == 'X': t2 -= 2 if s2[c2+1] == 'V': t2 -= 2 elif j == 'V': t2 += 5 elif j == 'X': t2 += 10 if c2+1 < len(s2): if s2[c2+1] == 'L': t2 -= 20 if s2[c2+1] == 'C': t2 -= 20 elif j == 'L': t2 += 50 elif j == 'C': t2 += 100 c2 += 1 if c2 == len(s2): break t3 = t1 + t2 print 'Value of %s: %s' %(s2,t2) print 'Value of %s + %s = %s' %(t1,t2,t3) s3 = '' while t3 > 0: if t3 >= 100: s3 += 'C' t3 -= 100 elif t3 >= 10: if t3 == 40: s3 += 'XL' t3 -= 40 elif t3 == 90: s3 += 'XC' t3 -= 90 elif t3 == 10: s3 += 'X' t3 -= 10 elif t3 == 50: s3 += 'L' t3 -= 50 elif t3 > 0: if t3 == 1: s3 += 'I' t3 -= 1 elif t3 == 3: s3 += 'III' t3 -= 3 elif t3 == 6: s3 += 'VI' t3 -= 6 elif t3 == 7: s3 += 'VII' t3 -= 7 elif t3 == 8: s3 += 'VIII' t3 -= 8 elif t3 == 4: s3 += 'IV' t3 -= 4 elif t3 == 5: s3 += 'V' t3 -= 5 elif t3 == 9: s3 += 'IX' t3 -= 9 if t3 == 0: break print 'The sum of %s + %s = %s' %(s1,s2,s3)

### #4

## Re: Why is this in an infinite loop?

Posted 09 October 2011 - 06:41 PM

What if t3 is 45, 46, 47? What if it is between 50 and 100? What I am trying to show you here is that you are not going to be able to solve this by merely testing t3 for each value and appending the roman numeral.

What you need to do is change the approach. How about taking a number, checking what the largest roman numeral fits into it, then subtracting that value out of the number and repeat until you are 0?

For instance if I had the number 51. I would test "Does C (100) fit into it?" No, so test the next biggest numeral. "Does L (50) fit into it?" Yes, subtract 50 out of 51 leaving 1 and put "L" into a string. Now you have 1, does "V" fit? no. does "I" fit? yes, so you subtract 1 from 1 and you get zero, you add the "I" onto the string to give you "LI".

Play with this idea. Just as a tip, some of the numerals you want to check for include "CD" (400) or "IX" (9) etc. The numbers you subtract out can be groups of roman numerals like this. It doesn't have to be one letter numerals.

What you need to do is change the approach. How about taking a number, checking what the largest roman numeral fits into it, then subtracting that value out of the number and repeat until you are 0?

For instance if I had the number 51. I would test "Does C (100) fit into it?" No, so test the next biggest numeral. "Does L (50) fit into it?" Yes, subtract 50 out of 51 leaving 1 and put "L" into a string. Now you have 1, does "V" fit? no. does "I" fit? yes, so you subtract 1 from 1 and you get zero, you add the "I" onto the string to give you "LI".

Play with this idea. Just as a tip, some of the numerals you want to check for include "CD" (400) or "IX" (9) etc. The numbers you subtract out can be groups of roman numerals like this. It doesn't have to be one letter numerals.

This post has been edited by **Martyr2**: 09 October 2011 - 06:41 PM

### #5

## Re: Why is this in an infinite loop?

Posted 09 October 2011 - 06:53 PM

That was the idea behind it, so i guess im not sure why it isnt going from 51 subtracting 50 adding L to the string then going to 1 and adding I

Page 1 of 1