-2

I´m making a project for school and it is a dice game. This snippet of my code is like a catch 22.

I need to define a variable otherwise it flags, so I do this but then every time the button is run it changes the value to zero instead of increasing it.

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()

snippet https://pastebin.com/FSWwBGpA

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    Can you revamp your question so that it defines the problem accurately? For example, the title does not provide any information at all to describe the problem you are trying to solve -- https://stackoverflow.com/help/how-to-ask – moo Jan 01 '20 at 16:44
  • Are roll1 & roll2 inside gamerun or are the indents you're showing incorrect? – DarrylG Jan 01 '20 at 17:03
  • yes they are inside gamerun – Matthew Goodman Jan 01 '20 at 17:06
  • Can you please cut down the code to the relevant parts? It is impossible to run this snippet without the rest anyway. – Jan Christoph Terasa Jan 01 '20 at 17:06
  • snippet: https://pastebin.com/FSWwBGpA – Matthew Goodman Jan 01 '20 at 17:15
  • all: https://pastebin.com/RgXfWEdU – Matthew Goodman Jan 01 '20 at 17:16
  • This could answer your question: [nested function change variable in an outside function not working](https://stackoverflow.com/questions/41278818/nested-function-change-variable-in-an-outside-function-not-working). Basically you need to assign Rollnop1 = 0 and Rollnop2 = 0 in gamerun and declare them as nonlocal inside roll1 & roll2 before attempting to change their value. – DarrylG Jan 01 '20 at 18:23
  • This really did answer my question thank you. – Matthew Goodman Jan 01 '20 at 22:30

2 Answers2

0

Use an option where you provide the player as part of the roll, that way you say which player is playing at any given time. The function below plays for the provided player and returns who is playing next

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
Billcountry
  • 585
  • 5
  • 6
  • The function is re-ran every time it is used so the variable is redefined every time it is ran. So all I get is : User 1 10 Console: Random Number 2 = 9 User 1 9 Console: Random Number 2 = 12 User 1 12 – Matthew Goodman Jan 01 '20 at 17:04
  • I'm suggesting you update the variable when you call the function roll. i.e. ```Rollnop1 = roll(Rollnop1)``` This way it will be updated to the return value which you need to update as needed – Billcountry Jan 01 '20 at 17:10
  • no that isnt working, its showing the same as before, only player one's scores show up. Here is the pastebin – Matthew Goodman Jan 01 '20 at 17:32
0

This could answer your question: nested function change variable in an outside function not working. Basically you need to assign Rollnop1 = 0 and Rollnop2 = 0 in gamerun and declare them as nonlocal inside roll1 & roll2 before attempting to change their value.

– DarrylG Thank You so much and to everyone else who helped.

More here nested function change variable in an outside function not working