【问题标题】:Is it possible to use a variable then later redefine it?是否可以使用变量然后重新定义它?
【发布时间】:2020-04-20 13:57:22
【问题描述】:

我正在为学校制作一个项目,这是一个骰子游戏。我的代码的这个 sn-p 就像一个 catch 22。

我需要定义一个变量,否则它会标记,所以我这样做了,但是每次运行按钮时,它都会将值更改为零而不是增加它。

if Rollnop1 == 0 :
    Userscore1 = Randomnumber
    print ("User 1 ",Userscore1 )

    Rollnop1 = Rollnop1+1 #But this changes it so it will go to the next players roll, every 
    #time the button is pressed it changes the variable back to 0
def gamerun():
    global Player
    global usernamestr
    global passwordstr
    global usernamestr2
    global passwordstr2
    Rollnop1 = 0

    def roll2():
        Rollnop2 = 0 
        Randomnumber = random.randint(2,12)
        print ("Console: Random Number 2 = ",Randomnumber)

        if Rollnop2 == 0 :
            Userscore2 = Randomnumber
            print ("User 2 ",Userscore2 )

    def roll1():
        Rollnop1 = 0 #Need to define this here otherwise It wont work
        Randomnumber = random.randint(2,12)
        print ("Console: Random Number = ",Randomnumber)

        if Rollnop1 == 0 :
            Userscore1 = Randomnumber
            print ("User 1 ",Userscore1 )
            Rollnop1 = Rollnop1+1 #But this changes it so it will go to the next players roll, every 
                                  #time the button is pressed it changes the variable back to 0

        else:
            roll2()

    actdicegame = Tk()
    gamerunl0 = Label(actdicegame, text = usernamestr, fg = "black")
    gamerunl0.pack()
    gamerunl1 = Label(actdicegame, text = "Roll The Dice", fg = "black")
    gamerunl1.pack()
    gamerunb1 = Button(actdicegame, text="ROLL",fg="Black", command=roll1)#Register Butto
    gamerunb1.pack()

    actdicegame.geometry("350x500")
    print ("Console: GUI RUNNING 1")
    actdicegame.mainloop()

sn-p https://pastebin.com/FSWwBGpA

【问题讨论】:

  • 您能否修改您的问题,以便准确定义问题?例如,标题根本没有提供任何信息来描述您要解决的问题——stackoverflow.com/help/how-to-ask
  • roll1 和 roll2 是否在 gamerun 中,或者您显示的缩进不正确?
  • 是的,他们在游戏运行中
  • 你能把代码删减到相关部分吗?无论如何,如果没有其余部分,就不可能运行这个 sn-p。

标签: python


【解决方案1】:

使用一个选项,将玩家作为滚动的一部分提供,这样您就可以说出在任何给定时间哪个玩家在玩。下面的函数为提供的播放器播放并返回下一个播放器

def roll(Rollnop=0):
    UserScore = random.randint(2,12)
    print ("Console: Random Number 2 = ", UserScore)

    if Rollnop == 0 :
        print ("User 1 ", UserScore)
        return 1
    else:
        print ("User 2 ", UserScore)
        return 0

【讨论】:

  • 该函数每次使用时都会重新运行,因此每次运行时都会重新定义变量。所以我得到的是:用户 1 10 控制台:随机数 2 = 9 用户 1 9 控制台:随机数 2 = 12 用户 1 12
  • 我建议您在调用函数 roll 时更新变量。即Rollnop1 = roll(Rollnop1) 这样它将更新为您需要根据需要更新的返回值
  • 不,这不起作用,它的显示和以前一样,只有玩家的分数显示。这是pastebin
【解决方案2】:

这可以回答您的问题:外部函数中的嵌套函数更改变量不起作用。基本上,您需要在游戏运行中分配 Rollnop1 = 0 和 Rollnop2 = 0 并在尝试更改它们的值之前在 roll1 和 roll2 中将它们声明为非本地。

– DarrylG 非常感谢您以及所有提供帮助的人。

这里有更多 nested function change variable in an outside function not working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2019-02-19
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多