Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 235 236 [237] 238 239 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 906713 times)

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3540 on: November 22, 2012, 06:52:04 pm »

In c++, global variables are file scope by default. But you can use 'extern' in other files to put that variable in that file's scope as well.
Which would be bad if you had 3 files:
File1:
int FUBAR;
File2:
int FUBAR;
File3:
extern int FUBAR;

SUDDENLY, LINKER ERRORS. And if you happen to be working on a project with a dozen other coders, all of whom are using global variables, that could get messy. Or it could be even worse:
File1: //implemented by programmer A
int FUBAR;
File2: //was going to be implemented by programmer B before he forgot
File 3: //implemented by programmer B
extern int FUBAR;
No compile errors, but you are now referencing some other variable the other guy put in. Which breaks everything.

Though that isn't really the main reason. It's mostly because you lose any ability to have centrallized control over the variable. Now anyone, anywhere can access your variable. And the only way to figure out where they are using it is Ctrl+F the entire project.
« Last Edit: November 22, 2012, 06:53:55 pm by alway »
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3541 on: November 23, 2012, 05:13:44 am »

Running this function:
Code: [Select]
def loop(typed=None):
    while typed != 'q': #<= line 7 in simple.py
    <rest>
Gives me this error:
Quote
Message   File Name   Line   Position   
Traceback            
    <module>   C:\Users\s4197887.RU\Downloads\Families\Families\families.py   39      
    main   C:\Users\s4197887.RU\Downloads\Families\Families\families.py   37      
    loop   C:\Users\s4197887.RU\Downloads\Families\Families\simple.py   7      
UnboundLocalError: local variable 'typed' referenced before assignment            

How is this possible? This even happens if I start the function with 'typed = None'.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3542 on: November 23, 2012, 06:48:34 am »

Well that is weird.
I just tested the following
Code: [Select]
def foo(bar = None):
    if (bar is None):
        print "Bar is None"
    else:
        print bar

And it works fine.
Can I see how you are trying to call the method?

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3543 on: November 23, 2012, 06:50:29 am »

Code: [Select]
# families.py
import simple

def main(years):
    <unrelated stuff>
    simple.loop()
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3544 on: November 23, 2012, 06:53:14 am »

Interesting.
Try running just your simple module in the Python Shell, and typing in loop(), see what it spits out.

EDIT:
Ok like 10% chance this will fix your problem. Worse than last time I had a hunch, but worth a shot!
Code: [Select]
while (typed != 'q'):

I'm pretty sure Python is meant to be smarter than that, but who knows.

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3545 on: November 23, 2012, 06:56:48 am »

Hmm, the program (families.py)actually works using Python Shell, I am using portable PyScripter btw, as I am at my uni.
Is there a way to make Python Shell accept string using 'x = input()'? As it still crashes, but now because of that.

edit: PyScripter does work when just calling loop() from simple.py. Odd.
« Last Edit: November 23, 2012, 06:58:42 am by Dutchling »
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3546 on: November 23, 2012, 07:00:34 am »

Do you assign to typed further down in the function?
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3547 on: November 23, 2012, 07:05:18 am »

Yes, but only using input() , and it crashes before that.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3548 on: November 23, 2012, 07:08:16 am »

I'm extremely rusty on python, but it could be a scope issue due to that assignment. Try commenting it out?
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3549 on: November 23, 2012, 07:08:34 am »

It is getting too late at night to be doing python. The language just gives you waaay too much rope to hang yourself!
Clearly there is a problem with your main somehow, otherwise it would also give that error when calling just that method. Pot shot time? Sure, why not. Fuck you proper testing methods!

1% chance to to cause: Do you use typed anywhere outside of the loop method?

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3550 on: November 23, 2012, 07:11:32 am »

Well, the error is probably just because of the portable PyScripter I am using, as Python Shell doesn't crash.
Well, it does crash, but only after the whole typed stuff, so it's not that. Is there a way to actually see the error message? Shell closes too fast for me to read it >.>
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #3551 on: November 23, 2012, 07:13:01 am »

Well, the error is probably just because of the portable PyScripter I am using, as Python Shell doesn't crash.
Well, it does crash, but only after the whole typed stuff, so it's not that. Is there a way to actually see the error message? Shell closes too fast for me to read it >.>

open the shell first. then run your program. the shell and its output should remain open when the program errors out.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3552 on: November 23, 2012, 07:14:00 am »

Well, the error is probably just because of the portable PyScripter I am using, as Python Shell doesn't crash.
Well, it does crash, but only after the whole typed stuff, so it's not that. Is there a way to actually see the error message? Shell closes too fast for me to read it >.>
Oh ok. Wait, your shell closes?
You mean the thing that looks like this?
Spoiler (click to show/hide)

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3553 on: November 23, 2012, 07:16:11 am »

Wait, I thought that was IDLE? I meant the thing that looks like this:
Spoiler (click to show/hide)
I can't actually use IDLE right now, as I am at my uni.

And the problem has been fixed, I have no idea why, as the exact code which causes errors earlier on now works.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3554 on: November 23, 2012, 07:19:41 am »

Now I could be making a fool of myself, but I'm oretty sure that the Command line and the IDLE are both shells.
Also, why would your school not have it? Seriously??!!?? Have you checked? It should be there!
Pages: 1 ... 235 236 [237] 238 239 ... 796