Python "Key Error"

A forum for general discussion of the Python programming language.

Python "Key Error"

Postby a9balsza » Fri Sep 23, 2016 11:06 am

i read an .fcl file and when i run the program it writes "Key Error"

Code: Select all
import fuzzy.storage.fcl.Reader
system 
= fuzzy.storage.fcl.Reader.Reader().load_from_file("Ballpositioner2.fcl")
import serial


my_input 
= {
        "REQ_POS" : 0.0,
        "DESIRED" : 0.0,
        }
my_output = {
        "PNEU" : 0.0
        
}

while True:
        # set input values
        my_input["REQ_POS"] =  10
        my_input
["DESIRED"] = 20
 
       
        system
.calculate(my_input, my_output)
 
 
        b 
= my_output["PNEU"]




it writes:

line 3:0 extraneous input u'VAR_INPUT' expecting Identifier

Code: Select all
Traceback (most recent call last):
 \sdfsdfsdfsd.py", line 22, in <module>
    system.calculate(my_input, my_output)
  \System.py"
, line 93, in calculate
    self
.defuzzify(output)
 \System.py", line 76, in defuzzify
    output[name] = self.variables[name].getValue()
KeyError: 'PNEU'
Last edited by micseydel on Fri Sep 23, 2016 3:21 pm, edited 1 time in total.
Reason: Code tags.
a9balsza
 
Posts: 13
Joined: Sat Jul 30, 2016 1:46 pm

Re: Python "Key Error"

Postby a9balsza » Fri Sep 23, 2016 4:37 pm

in python shell if i write "print my_output.keys()" it writes PNEU
a9balsza
 
Posts: 13
Joined: Sat Jul 30, 2016 1:46 pm

Re: Python "Key Error"

Postby ichabod801 » Fri Sep 23, 2016 11:17 pm

a9balsza wrote:in python shell if i write "print my_output.keys()" it writes PNEU

Yes, but the error is in system.variables, not in my_output. That would seem to indicate that the file you are reading from does not have the PNEU information.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Python "Key Error"

Postby a9balsza » Sat Sep 24, 2016 7:40 am

And why it writes this problem for only the output?
a9balsza
 
Posts: 13
Joined: Sat Jul 30, 2016 1:46 pm

Re: Python "Key Error"

Postby ichabod801 » Sat Sep 24, 2016 12:01 pm

It's not writing it for the output. It's assigning to the output. You won't get a key error on assignment. If the key isn't there during assignment the key just gets added. The error is when it's accessing self.variables[name]. On access it will give the key error if the key isn't there. The traceback shows this is happening during system.calculate, so self is equal to system, and thus system.variables does not have the key 'PNEU'. The system variable is assigned when reading the file, leading to the conclusion that 'PNEU' is not in the file.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Python "Key Error"

Postby Yoriz » Sat Sep 24, 2016 12:20 pm

Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 8 guests