Be a Supporter!

Python v3 NameError Bug

  • 385 Views
  • 4 Replies
New Topic Respond to this Topic
zoke
zoke
  • Member since: Aug. 16, 2007
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Python v3 NameError Bug 2012-01-05 22:29:11 Reply

Hey NG:

Long story short, I'm trying to write a Python code for an IRL luck game I made a while back. However, it keeps giving me NameErrors based on a set of variables that I've already defined. The relevant bits are as follows (sorry if it's formatted badly, this is my first post using the "code" html):

import random
random.seed()
bank = float(0.00)
def roll_1or2():
    cval = random.randint(1,2)
    d1val = random.randint(1,6)
    d2val = random.randint(1,6)
    if cval == 1:
        cval = "Heads"
        return cval
    else:
        cval = "Tails"
        return cval
    return d1val
    return d2val
def mainprog():
    throwconf = input("Addem once? ('yes' or 'no')")
    if throwconf == "yes":
        roll_1or2()
        print('         ...the coin lands on', cval)
        print('         ...the first die lands on', d1val)
        print('         ...the second die lands on', d2val)
        if cval == "Tails":
            bank = bank - 5.00
            return bank
            games = games + 1
            return games
            print("loser")
        else:
            print("winner")
    elif throwconf == "no":
        home()
    else:
        throwconf = input("Command invalid. Please retype.")

It's fine until the bit in mainprog when it has to input the cval. It gives me:

Traceback (most recent call last):
  File "[filepath]", line 54, in <module>
    mainprog()
  File "[filepath]", line 15, in mainprog
    print('         ...the coin lands on', cval)
NameError: global name 'cval' is not defined

It's treating the code like roll_1or2() isn't even in there. I've tried defining cval globally first as 0, but then it returns a 0 for the cval in mainprog and ignores the return from roll_1or2. And even if it gives me a bad output like that, all it does is move on to d1val and do the same thing over again. Same with d2val if d1val is globally defined. What is this getting tripped up on? I had this working beautifully at one point... it return a H/T value for the coin and a good numerical value for the dice... and then I did something... and now it won't work. Any help?"


PM me for a sig like this.
/saying "/thread"

BBS Signature
zoke
zoke
  • Member since: Aug. 16, 2007
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Python v3 NameError Bug 2012-01-06 00:08:19 Reply

Never mind. I decided it would just be easier to globalize the vars in the first place, although I would much prefer to be able to subroutine them. I still wish I knew why it wouldn't return the vars back to global though... oh well. Problem solved.


PM me for a sig like this.
/saying "/thread"

BBS Signature
zoke
zoke
  • Member since: Aug. 16, 2007
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Python v3 NameError Bug 2012-01-06 00:17:16 Reply

No, spoke too soon (kind of talking to myself here). Now it's keeping the variables intact, but it's making them a static random number. Does anyone know how to make them a new random number each time they're called?


PM me for a sig like this.
/saying "/thread"

BBS Signature
sharpnova
sharpnova
  • Member since: Feb. 19, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Python v3 NameError Bug 2012-01-06 07:25:28 Reply

Just a quick question. Why are you doing this in Python?


= + ^ e * i pi 1 0

everette00
everette00
  • Member since: Nov. 13, 2008
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Python v3 NameError Bug 2012-01-06 12:27:45 Reply

I found a few problems with the code snippet your provided. First, you're going to run into problems with returning data from the function "roll_1or2". You are returning two values, but Python is designed to stop after the first return, so the second return will just be dead code and will not do anything.

Second, the reason you're getting errors is because you're attempting to use variables that are local-scope to "rol_1or2" as if they're local to "mainprog()". They aren't. This is where you're having the problem specifically:

print('         ...the coin lands on', cval)
print('         ...the first die lands on', d1val)
print('         ...the second die lands on', d2val)

Third, you're doing the same thing with returns in "mainprog" that you did with "roll_1or2". A function/method ends with its return or, if you didn't provide one, the last line. You have some major reconstruction needed for this portion of the program.

You may want to do something like this:

import random

class Game() :
    bank = 0
    cval = random.randint(1, 2)
    d1val = random.randint(1, 6)
    d2val = random.randint(1, 6)
    
    def roll_1or2(self) :
        if self.cval == 1 :
            self.cval = "Heads"
        else :
            self.cval = "Tails"
    
    def doChance(self) :
        throwconf = input("Addem once? ('yes' or 'no')")
        
        if throwconf == "yes" :
            roll_1or2()
            print('... the coin lands on', self.cval)
            print('... the first die lands on', self.d1val)
            print('... the second die lands on', self.d2val)
            
            if self.cval == "Tails" :
                bank = bank - 5
                games = games + 1
                print("loser")
            
            else :
                print("winner")
                
        elif throwconf == "no" :
            home()
            
        else :
            throwconf = input("Command invalid. Please retype.")