是什么阻止了这些变量在我的代码中的定义?

时间:2014-10-23 22:54:44

标签: function python-2.7 variables tkinter global-variables

我一直在设计这个游戏,我遇到了一个奇怪的问题。现在也许这只是我错过的一些简单的事情,或者我将其格式化的方式。当我运行我的代码时,我得到这些错误,表明我所定义的变量都没有被声明/定义。最重要的是,似乎没有任何函数运行...我应该将text1,button1Name等定义为全局变量吗?或者是他们更好的方式?

##Project 1 v4
##Structure whole program with base functionality
##Begin with an outline then fill in functions and windows from V2, and V3.
import Tkinter
import random

inventory=[]
playCount=0


def startGame():
    
    ##adds name
    ##button that calls StoryCard
    
    text1="Welcome to Treasure Quest!! The Game that allows you to pick your own destiny! Treasure quest is a very simple game. The story will display on the screen until it reaches an event. At each event you will be shown two choices. Each decision may cause you to leave with more treasure or to die a horrible death... Still like any true or false question on a final exam, you only have a 50/50 chance of ruining your entire life if you do not know! Now that you know how to play, please click the button below to start the game!" 
    button1= introCard()
    button1Name="Play"
    
    button2= win.destroy
    button2Name="Quit"
    
    
def introCard():
    text1="You are the bravest knight in the service of the great kingdom Universitas! The kingdom has long been an icon throughout the world, but now it faces great peril. The king of Universitas has foolishly spent the entirety of the kingdoms fortune shopping online. The only hope for the kingdom, is to find the money to pay off it's debts. Luckily recently some scrolls have been found that describe the locations of hidden treasures. You have been selected to find these treasures. Do you except?"
    button1=winCard()
    outcome1="You embark upon your quest!"
    button1Name="Accept!"
        
    button2=lossCard()
    outcome2="The kingdom falls into debt and you die of dysentary... Nice going Oregon Trail..."
    button2Name="Decline!"




    
def storyCard(n):##This function will randomly select a story chunk
    ##recieves a random int    
    ##tests for true int w/ if then else line
    ##Story blurbs for these are stored in V2 copy them!
    if n ==1:
        text1="You come across a dark cave... A dragon is said to lurk within... You enter and see the Holy Sword... How do you take it?"
        button1=lossCard()
        outcome1="The dragon bakes you to a crisp... Sorry!"
        button1Name="Bargain for it!"
        
        button2=winCard()
        outcome2="Victory! You sneak past the dragon and make off with the sword!"
        button2Name="Steal it!"
        #get="Holy Sword"
        
    elif n==2:
        text1="You come across a mummys tomb... You see two doors. Take the front entrance or the hidden entrance?"
        button1=winCard()
        outcome1="The mummy is awake and glad for company! He feeds you tea and sets you on your way with gifts!"
        button1Name="Front"
        #get="A beautiful glass vase!"
        
        button2=lossCard()
        outcome2="The mummy sees you! It screams thief! and shivs you..."
        button2Name="Hidden"

    elif n==3:
        text1="You come across a magic tree inhabited by elves. They ask you to climb up and see their beautiful home."
        button1=lossCard()
        outcome1="You climb high but slip off a branch and fall to your death. The elves laugh."
        button1Name="Climb"
        
        button2=winCard()
        outcome2="The elves are perplexed as to why you do not want to see them. They come down and give you a new couch to show off their things."
        button2Name="Stay"
        #get="A rad couch"

    elif n==4:
        text1="You come across a cave filled with strange lights! A troll stands guard." 
        button1=winCard()
        outcome1="This is a hip and happening new dwarven nightclub! The troll lets you in because you seem cool! The dwarves give you some rare cave mushrooms. It seems lame but they said the kings a regular and they will go over well. You have a fun night... You drink to much ale and get a late start the next morning though..."
        button1Name="Strut in!"
        #get="Rare Mushrooms"
        
        button2=winCard()
        outcome2="This is actually a hip new dwarven nightclub! The bouncer troll won't let you in though... Oh well! dwarves are hipsters anyways! Gratefull for your company the troll gives you an amulet!"
        button2Name="Wait in line."
        #get="Sweet Amulet"

    elif n==5:
        text1="You come across a hut in the woods! Its owned by Merlin! The great wizard starts up conversation and invites you inside!"
        button1=lossCard()
        outcome1="Insulted, Merlin kills you with an axe... you would have expected magic... but no, Merlin is a psychopath and prefers to murder with axes."
        button1Name="Leave"

        button2=winCard()
        outcome2="He talks about his GOD DAMNED GRANDKIDS FOR FOUR HOURS... He is grateful for you company however and lets you leave with his staff."
        button2Name="Enter"
        #get="A sweet Staff"
    
    ## each if, elif, ect must contain
        ##Text blurb
        ##Button 1 function def
        ##Button 2 function def
        ##button 1 name
        ##button 2 name
        ##Outcome 1 Text
        ##Outcome 2 Text

def winGame():
    text1="CONGRATULATIONS!! You have collected enough treasure to pay off the kingdoms debts! You will forever be known to the people as a great knight! The king is in your debt!"
    button1Name="Hooray!"
    button1=win.destroy
    button2Name="Huzzah!"
    button2=win.destroy

def winCard():##Describes how event played out
    #inventory.append[get]
    playCount+=1
    text1= outcome1 or outcome2
    if playCount == 5:
        button1Name="Onward!"
        button1=winGame()
        button2Name="Go Forth!"
        button2=winGame()
    else:
        button1Name="Onward!"
        button1=storyCard(random.randint(1,5))
        button2Name="Go Forth!"
        button2=storyCard(random.randint(1,5))

    
def lossCard():
    text1=outcome1 or outcome2
    button1Name="Game Over"
    button1= win.destroy
    button2Name="Game Over"
    button2= win.destroy
   


    

startGame()

win=Tkinter.Tk()
win.title("TREASURE QUEST!!")
Textlabel=Tkinter.Label(win,text = text1,font=('Times New Roman',12),justify=LEFT,)
Textlabel.pack()
Row2=Tkinter.Frame(win)
Btn1=Tkinter.Button\
  (Row2, text=button1Name, command=button1,font=('Times New Roman',12))
Btn2=Tkinter.Button\
  (Row2, text=button2Name, command=button2, font=('Times New Roman',12))
Btn1.pack(side='left')
Btn2.pack(side='left')
Row2.pack()

win.mainloop()

当我运行代码时,我遇到了这些错误:

Traceback (most recent call last):
  File "C:/Users/Logan/Desktop/Project#1V4.py", line 141, in <module>
    startGame()
  File "C:/Users/Logan/Desktop/Project#1V4.py", line 17, in startGame
    button1= introCard()
  File "C:/Users/Logan/Desktop/Project#1V4.py", line 26, in introCard
    button1=winCard()
  File "C:/Users/Logan/Desktop/Project#1V4.py", line 116, in winCard
    playCount+=1
UnboundLocalError: local variable 'playCount' referenced before assignment

1 个答案:

答案 0 :(得分:3)

如果要更改函数中的全局变量,则需要使用关键字global,如下所示 -

def winCard():##Describes how event played out
    global playCount
    #inventory.append[get]
    playCount+=1