Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56737

Re: Why isn't this code working how I want it to?

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Why isn't this code working how I want it to?
Date 2013-10-12 10:39 +0100
References <f74fadac-e7c3-4419-9a65-ed6ecd5eb3fe@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1033.1381570800.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/10/2013 09:56, reubennottage@gmail.com wrote:
> I've been working on a program and have had to halt it due a slight problem. Here's a basic version of the code:
>
> a = 'filled'
> b = 'filled'
> c = 'empty'
> d = 'empty'
> e = 'filled'
> f = 'empty'
> g = 'filled'
>
> testdict = {a : 'apple' , b : 'banana' , c : 'cake' , d : 'damson' , e : 'eggs' , f : 'fish' , g : 'glue'}
>
> Now what I want to do, is if a variable is filled, print it out. This however isn't working how I planned. The following doesn't work.
>
> for fillempt in testdict:
>      if fillempt == 'filled':
>          print(testdict[fillempt])
>
> All this does though, is print glue, where I'd want it to print:
>
> apple
> banana
> eggs
> glue
>
> Perhaps a dictionary isn't the best way to do this.. I wonder what else I can do...
>
> Thanks for any help.
>

You've effectively set up a dictionary with keys 'filled' and 'entries' 
which you can see if you run this loop

for key, value in testdict.items():
     print(key, value)

which gives me this

empty fish
filled glue

I'm too lazy to type anything else so please refer to this 
http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python. 
  I'll also leave the argument over whether it's a variable or a name to 
others :)

-- 
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why isn't this code working how I want it to? reubennottage@gmail.com - 2013-10-12 01:56 -0700
  Re: Why isn't this code working how I want it to? Marco Nawijn <nawijn@gmail.com> - 2013-10-12 02:17 -0700
  Re: Why isn't this code working how I want it to? Peter Otten <__peter__@web.de> - 2013-10-12 11:20 +0200
    Re: Why isn't this code working how I want it to? reubennottage@gmail.com - 2013-10-12 04:03 -0700
      Re: Why isn't this code working how I want it to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-12 12:13 +0100
  Re: Why isn't this code working how I want it to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-12 10:39 +0100
  Re: Why isn't this code working how I want it to? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-12 12:50 +0300

csiph-web