0

To preface, I'm very new to coding with python and coding period. I've made a dice roller in python which allows the user to decide the number of sides, as well as the number of dice rolled. The code works as intended, but I keep getting an error message whenever I do multiple re-rolls and exit out. I've posted a screenshot of the error message and copied over the code too. I'd really appreciate any feedback, and thanks for reading this far. Error Message

## Roller v2

def Roll2():
    print("""
    [ - - - Welcome to Roller v2! - - - ]
    """)
    sides = int(input("How many sides? : "))
    dice = int(input("How many dice? : "))
    modifier = input("Any Modifiers (Include +/-) : ")

    def roller():
        import random as rand
        from random import choices
        d_inf = range(1, sides+1)
        d_choice = choices(d_inf, k=dice)
        print("""Wowee, you rolled :
        """)
        print(d_choice)
        print(""" """)
        print("Unadded Modifiers : (" + modifier + ")")

        repeat = str(input("Would you like to roll the same dice again? (y/n) : "))

        if repeat in ["y", "Y"]:
            roller()
        elif repeat in ["n", "N"]:
            exit = str(input("Would you like to exit? (y/n) : "))
        
        if exit in ["y", "Y"]:
            print("Goodbye!\n")
        elif exit in ["n", "N"]:
            Roll2()
  
    roller()
souschef
  • 11
  • 1
  • Please type out the error message and describe what you expect to happen. Also make a [MRE] e.g. don't ask for user input but hard code the values for which you get the error – peer Jul 27 '21 at 23:18
  • Does this answer your question? [Local variable referenced before assignment?](https://stackoverflow.com/questions/18002794/local-variable-referenced-before-assignment) – Woodford Jul 27 '21 at 23:19
  • @Woodford I don't think that answers the question; he's using it locally, not globally. As it says in John Gordon's answer, this is caused by the original function continuing to run after it has been called recursively. – ᴓᴓᴓ Jul 27 '21 at 23:27

2 Answers2

1
if repeat in ["y", "Y"]:
    roller()
elif repeat in ["n", "N"]:
    exit = str(input("Would you like to exit? (y/n) : "))
    
if exit in ["y", "Y"]:
    ...

If repeat is "y", roller() is called recursively. And when the recursive call is finished, the original call continues executing with the if exit in statement, and in that execution of the function, the variable exit was never defined.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

Just do this:

def roll2():

    print("""
    [ - - - Welcome to Roller v2! - - - ]
    """)
    sides = int(input("How many sides? : "))
    dice = int(input("How many dice? : "))
    modifier = input("Any Modifiers (Include +/-) : ")

    def roller():
        import random as rand
        from random import choices
        d_inf = range(1, sides + 1)
        d_choice = choices(d_inf, k=dice)
        print("""Wowee, you rolled :
        """)
        print(d_choice)
        print(""" """)
        print("Unadded Modifiers : (" + modifier + ")")

        repeat = str(input("Would you like to roll the same dice again? (y/n) : "))

        if repeat in ["y", "Y"]:
            roller()
        elif repeat in ["n", "N"]:
            exit = str(input("Would you like to exit? (y/n) : "))

            if exit in ["y", "Y"]:
                print("Goodbye!\n")

            else:
                roll2()
    return roller()

roll2()
Francisco Wendel
  • 618
  • 2
  • 5
  • 13