Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2009-12-23 14:32:30

simron
Real Member
Registered: 2006-10-07
Posts: 237

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:

import random

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

Here's what I get when I run it:

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

Offline

#2 2009-12-24 07:44:26

simron
Real Member
Registered: 2006-10-07
Posts: 237

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

Offline

#3 2009-12-25 03:48:15

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

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..."

Offline

#4 2009-12-26 06:50:48

simron
Real Member
Registered: 2006-10-07
Posts: 237

Re: Python Variable Definition Troubles

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


Linux FTW

Offline

#5 2010-01-15 18: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-15 18: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