0

I'm trying to write a die-rolling program, but I keep getting the Unbound error. I tried putting 'global in front of endGame, but it didn't seem to fix my issue, as well as re-arranging my pseudocodes many different ways. Are there any suggestions as to how I fix my syntax so I don't get this error? Thanks!

'global' now says that my endGame variable is undefined.

# Serra Jefferys
# 04/17/2023
# Python 07: Dice Roll
# Program simulated rolling dice using RANDOM fnctn. 2 Players enter name; Highest value wins; Rolls unlimited; use value-returning functions.

import random
min = 1
max = 6

def main():
    
    # Enter Player 1 and 2 names
    player_1 = input("Enter Player 1 name: ")
    print("Player 1: " + player_1)
    player_2 = input("Enter Player 2 name: ")
    print("Player 2: " + player_2)

    global endGame
    while endGame == "N" or endGame == "n":
        # roll Player 1
        p1Roll = random.randint(1, 6)
        print(f"{player_1}'s roll is {p1Roll}")
        # roll Player 2
        p2Roll = random.randint(1, 6)
        print(f"{player_2}'s roll is {p2Roll}")
    
        # Declare Winner
        if p1Roll == p2Roll:
           print ("This game is a tie!")
        elif p1Roll > p2Roll:
            print ("The winner is" + player_1 + "!")
        else:
            p1Roll < p2Roll
            print ("The winner is " + player_2 +"!")
        
    endGame = input ("Do you want to end the game? Y/N (N = no):")
main ()

Tried 'global' fix, rearranging code and indents, and various tweaks.

  • 1
    What value do you expect `endGame` to have on the first iteration of your `while` loop? Where do you expect that value to come from? – jasonharper Apr 17 '23 at 23:24

0 Answers0