Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

#1 2009-12-24 13:32:30

simron
Real Member

Offline

Python Variable Definition Troubles

HELP!
I'm trying to make a Mafiya game in Python, but have run into problems. Here's what I'm worried about:

Code:

import random

def Start():
    day = 0 #This variable definition should work...
    DayTime()
...

Here's what I get when I run it:

Code:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    Start()
  File "/Users/Rayment/Documents/Chase/games/Python/Mafiya!", line 5, in Start
    DayTime()
  File "/Users/Rayment/Documents/Chase/games/Python/Mafiya!", line 7, in DayTime
    day = day + 1;
UnboundLocalError: local variable 'day' referenced before assignment

I know that means that my variable definition is wrong, but it should be right. What gives?


Linux FTW

#2 2009-12-25 06:44:26

simron
Real Member

Offline

Re: Python Variable Definition Troubles

Sorry to double post, but I noticed that a Windows machine with the same installation of Python doesn't have a single problem.


Linux FTW

#3 2009-12-26 02:48:15

Ricky
Moderator

Offline

Re: Python Variable Definition Troubles

Not a python programmer here, but it looks like day is declared locally instead of globally.  This means that it won't exist in other local routines.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

#4 2009-12-27 05:50:48

simron
Real Member

Offline

Re: Python Variable Definition Troubles

True, but that doesn't explain why it works on Windows, but not Mac.


Linux FTW

#5 2010-01-16 17:30:22

Rick
Guest

Re: Python Variable Definition Troubles

El error tipo "<pyshell#4>"es por error de impresion...

cambia el

print variable

que estes ejecutando por:

print str(variable)

debe funcionar, ojala te sirva

#6 2010-01-16 17:33:09

Rick
Guest

Re: Python Variable Definition Troubles

sorry... wrong language:

the error "<pyshell#4>" points to a print variable error

instead of:

print variable

use:

print str(variable)

I hope it can helps smile

Board footer

Powered by FluxBB