3 Replies - 227 Views - Last Post: 29 August 2013 - 07:35 AM Rate Topic: -----

#1 vasant_Gowda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-August 13

getting incorrect output

Posted 29 August 2013 - 05:54 AM

Hi friends,
My issue is for reading textfile and storing it in a dictionary format and comparing keys with user choices. my code goes here....
import string
legendfile = open('legend.map' , 'r')

legenddict = {}
	
print 
print '===================================================================='
print
	
for line in legendfile:
	if not line.startswith('#'):
		
		print 'Single Legend:'
		print line
		print
		
		line = line.expandtabs(1)
		line = line.replace(' ','')
		line = string.upper(line)
		
		c = line.split(':')
		legendname = c[0]
		colorlist = c[1]
		colors = colorlist.split(',')
				
		print 'LegendName-----Colorlist:'
		print legendname,'----',colorlist
		print
		print 'Colors:'
	        print colors
    		print
		
		legenddict[legendname] = colors
		
		print 'LegendDict:'
		print legenddict
		print
	
		print
		print '==========================================================================='
		print
		
		for legendname,colorlist in legenddict.items():
			print len(colorlist),'colors defined in Legend:',legendname
			for color in colorlist:
				print '	',color
			print 
				
		print
		print '==============================================================='
		print
		

choice = raw_input("Enter the legendname")
for legendname, colorlist in legenddict.iteritems():
	if choice == legendname:
		print  len(colorlist), 'Colors defined in Legend:', legendname
		for color in colorlist:
			print '    ', color
		break
		
	else:
		print "Invalid input"






I am Getting output as:

-bash-3.2$ python Example.py

====================================================================

Single Legend:
Standard_Animator:       blue,3f7fff,00bfff,3fffbf,green,bfff3f,ffbf00,ff7f00,red


LegendName-----Colorlist:
STANDARD_ANIMATOR ---- BLUE,3F7FFF,00BFFF,3FFFBF,GREEN,BFFF3F,FFBF00,FF7F00,RED


Colors:
['BLUE', '3F7FFF', '00BFFF', '3FFFBF', 'GREEN', 'BFFF3F', 'FFBF00', 'FF7F00', 'RED\n']

LegendDict:
{'STANDARD_ANIMATOR': ['BLUE', '3F7FFF', '00BFFF', '3FFFBF', 'GREEN', 'BFFF3F', 'FFBF00', 'FF7F00', 'RED\n']}


===========================================================================

9 colors defined in Legend: STANDARD_ANIMATOR
        BLUE
        3F7FFF
        00BFFF
        3FFFBF
        GREEN
        BFFF3F
        FFBF00
        FF7F00
        RED



===============================================================

Single Legend:
Extended_Animator:    LightGray,blue,3f7fff,00bfff,3fffbf,green,bfff3f,ffbf00,ff7f00,red,Magenta


LegendName-----Colorlist:
EXTENDED_ANIMATOR ---- LIGHTGRAY,BLUE,3F7FFF,00BFFF,3FFFBF,GREEN,BFFF3F,FFBF00,FF7F00,RED,MAGENTA


Colors:
['LIGHTGRAY', 'BLUE', '3F7FFF', '00BFFF', '3FFFBF', 'GREEN', 'BFFF3F', 'FFBF00', 'FF7F00', 'RED', 'MAGENTA\n']

LegendDict:
{'EXTENDED_ANIMATOR': ['LIGHTGRAY', 'BLUE', '3F7FFF', '00BFFF', '3FFFBF', 'GREEN', 'BFFF3F', 'FFBF00', 'FF7F00', 'RED', 'MAGENTA\n'], 'STANDARD_ANIMATOR': ['BLUE', '3F7FFF', '00BFFF', '3FFFBF', 'GREEN', 'BFFF3F', 'FFBF00', 'FF7F00', 'RED\n']}


===========================================================================

11 colors defined in Legend: EXTENDED_ANIMATOR
        LIGHTGRAY
        BLUE
        3F7FFF
        00BFFF
        3FFFBF
        GREEN
        BFFF3F
        FFBF00
        FF7F00
        RED
        MAGENTA


9 colors defined in Legend: STANDARD_ANIMATOR
        BLUE
        3F7FFF
        00BFFF
        3FFFBF
        GREEN
        BFFF3F
        FFBF00
        FF7F00
        RED



===============================================================

Enter the legendnameSTANDARD_ANIMATOR
Invalid input
9 Colors defined in Legend: STANDARD_ANIMATOR
     BLUE
     3F7FFF
     00BFFF
     3FFFBF
     GREEN
     BFFF3F
     FFBF00
     FF7F00
     RED


It is working for else part also can anybody help how to come out of this one

This post has been edited by baavgai: 29 August 2013 - 06:59 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: getting incorrect output

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: getting incorrect output

Posted 29 August 2013 - 06:14 AM

It's not very clear from your post what isn't outputting correctly. Were you wanting it to only say "Invalid input" in the event that the specified legend name isn't in the dictionary?
Was This Post Helpful? 0
  • +
  • -

#3 vasant_Gowda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-August 13

Re: getting incorrect output

Posted 29 August 2013 - 06:55 AM

In My output file WHEN i ENTER MY CHOICE AS sTANDARD_ANIMATOR(LEGENDNAME) IT IS SHOWING OUTPUT AS FOLLOWS

Enter the legendnameSTANDARD_ANIMATOR
Invalid input
9 Colors defined in Legend: STANDARD_ANIMATOR
BLUE
3F7FFF
00BFFF
3FFFBF
GREEN
BFFF3F
FFBF00
FF7F00
RED

bUT WHAT I EXPECT IS IT SHOULD PRINT ONLY
Enter the legendnameSTANDARD_ANIMATOR
9 Colors defined in Legend: STANDARD_ANIMATOR
BLUE
3F7FFF
00BFFF
3FFFBF
GREEN
BFFF3F
FFBF00
FF7F00
RED

NOT "INVALID INPUT". IN MY OUTPUT IT IS GIVING OUTPUT FOR ELSE PART ALSO.BUT I DON'T NEED THAT
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: getting incorrect output

Posted 29 August 2013 - 07:35 AM

So basically, your answer to my question was "Yes".

Fortunately, this is an easy fix. The way your code is currently structured, it checks each element in the dictionary, and it makes that comparison for each item in the dictionary. Because you only need information for the provided legend, the loop isn't actually necessary (and is essentially just extra overhead). You'll just want to check to see if the legend actually exists in your dictionary. This would replace the loop you've got on line 55:

if choice in legenddict:
    colorlist = legenddict[choice]
    print len(colorlist), 'Colors defined in Legend:', choice
    for color in colorlist:
        print '    ', color
else:
    print "Invalid input."


This way, instead of iterating over the whole dictionary to see if the provided legend exists, it just checks it directly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1