3 Replies - 879 Views - Last Post: 11 October 2011 - 05:25 AM Rate Topic: -----

#1 CodyWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-February 10

UnboundLocalError: local variable 'ch' referenced before assig

Posted 10 October 2011 - 02:42 PM

I am currently converting my C++ program to Python, but I can't get it. I want the code similar, so it looks almost exactly the same... resulting in problems.

My C++ code:
C++ Header File
Spoiler


Main C++ code:
Spoiler


My Python Code:
Spoiler


When I run my Python code, I get the error:

Quote

UnboundLocalError: local variable 'ch' referenced before assignment

But it is defined as a global variable at the top. So I tried referencing the global variables as follows:
Spoiler

And get the error:

Quote

AttributeError: 'str' object has no attribute 'number'


So what' the problem with these codes? I'm really dumfounded.

Is This A Good Question/Topic? 0
  • +

Replies To: UnboundLocalError: local variable 'ch' referenced before assig

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: UnboundLocalError: local variable 'ch' referenced before assig

Posted 10 October 2011 - 03:13 PM

You have the line strResult.number(ans). What makes you think that a string object (strResult) has a method called "number"? Did you need to convert it or something? You can use built in functions like int() or float() to convert to an integer or float value.


:)
Was This Post Helpful? 0
  • +
  • -

#3 CodyD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 19-August 10

Re: UnboundLocalError: local variable 'ch' referenced before assig

Posted 10 October 2011 - 03:24 PM

(Sorry about diff account, forgot this is my actual one.)

Well, I was converting straight from C++ and wasn't thinking about exactly what each line meant :) Oopsie. Let me try the float() really quick... I don't think that line is even needed in Python honestly.

I removed strResult and just tried using ans.

I get this error again:

Quote

UnboundLocalError: local variable 'ans' referenced before assignment

Still don't understand this one. It is assigned as the top as ans = 0.00 and referenced in the line as global ans ...

Now a new problem.

I got the basic math.. sortof working. But not. Explanation? Yes.

New code:
Spoiler


Problem?
9/3=0.0
9*3=0.0
9+3=0.0
9-3=0.0

See the problem? :) What is wrong now .-.

{Not to mention this error:

Quote

builtins.SystemExit
Traceback (most recent call last):
File "C:\Users\Cody Dostal\Programming\Anansi-WebCalc\Anansi-WebCalc\Program.py", line 172, in <module>
sys.exit(app.exec_())
int: 0

that happens EVERY time I exit the program.

This post has been edited by CodyD: 10 October 2011 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: UnboundLocalError: local variable 'ch' referenced before assig

Posted 11 October 2011 - 05:25 AM

In the future, please post the entire traceback, including line numbers and the code excerpt. Your traceback should look like this:

Traceback (most recent call last):
  File "wrong.py", line 8, in <module>
    foo()
  File "wrong.py", line 2, in foo
    rock = bar()
  File "wrong.py", line 6, in bar
    toast += brisket
NameError: global name 'brisket' is not defined



From above, we can see:
  • The module enteres the foo function on line 8
  • The foo function calls the bar function on line 2
  • In bar, there is at line 6 the code is toast += brisket
  • The variable brisket is not defined.


All of this is learned without looking at the code. Posting full tracebacks is essential to getting your question answered, especially in cases where you haven't or can't include the full code (what is mainAnan?).

This post has been edited by Motoma: 11 October 2011 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1