LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2014, 02:18 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
breaking loop in python


I am writing a code to convert integer to roman.There are a lot of solution available in internet. But I am writing in this way in python:

Code:
#!/usr/bin/python
import sys

def convertToRoman( iinteger ):

        values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 5, 4,  1 ]
        symbolValues = { 1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD', 90 : 'XC', 100 : 'C', 50 : 'L', 40 : 'XL', 10 : 'X', 9 : 'IX', 5 : 'V', 4 : 'IV', 1 : 'I' }
        romanNumberList = []
        for value in values:
                rvalue = int(value)
                integer = int(iinteger)
                print integer,":",rvalue
                while  integer >= rvalue :
                        print integer,":",rvalue
                        romanNumberList.append(symbolValues[rvalue])
                        print romanNumberList
                        integer = integer - rvalue
                        print integer

                print "while finshing :", integer
        romanValue = ''.join(romanNumberList)
        print iinteger, ":", romanValue




number = sys.argv[1]
num = int(number)
if ( num > 2999):
        print "Bigger than expected 2999....exiting";
        sys.exit(2);
else:
        convertToRoman(num)

here I am not able to break the for loop .

example:

if input is 6: 6 compare to 5, gettng V, then 6-5=1 and 1 compared to 1 and getting I.
Now I got the roman as VI. and the loop should exit there.

instead of this, it again comparing 6 with 4, 6 with 1 from the values list.

Can anyone help me in this ??

Last edited by divyashree; 11-16-2014 at 05:51 AM. Reason: question updated
 
Old 11-16-2014, 01:09 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Reverse your loops:
Code:
while input > 0
    loop over values
        if value <= input
            append roman numeral
            subtract value from input
            break
 
Old 11-16-2014, 01:52 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,096

Rep: Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365
the problem is the location of this line:
integer = int(iinteger)
this will reinitialize integer inside the for loop (and before the while cycle). This line should be put before the for cycle.

furthermore you missed the value 9 from the list of values, you ought to use:
for value in sorted(symbolValues.keys(),reverse=True):

Last edited by pan64; 11-16-2014 at 02:05 PM. Reason: additional info
 
Old 11-17-2014, 04:32 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,262

Rep: Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339
try... except is a commonly accepted hack to break out of nested loops.

That said, if you really find yourself needed to break out of two loops then your code should be rewritten.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Labeling tapes in shell for loop breaking asandybox Linux - General 6 11-07-2012 12:22 PM
Bash Loop - Breaking with hanging file descriptor caylan Programming 2 05-13-2012 09:55 AM
Breaking a loop from the keyboard. stf92 Programming 7 01-31-2012 05:31 PM
problem with comparison and breaking out of loop java xskycamefalling Programming 3 10-12-2010 09:11 AM
Python, breaking out of infinite loop edM Programming 7 05-05-2009 11:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration