0

Getting an error but I don't know why..(I'm learning) My code;

import random
input("Hit enter to roll the dice")
global answer
def rollDice():
    result = random.randrange(1,6)
    print ("It landed on.." + str(result))
    answer = input("would you like to play again? [y/n]")
rollDice();


if (answer == "y" or "Y"):
    rollDice();

Error; (some of the script works)

Hit enter to roll the dice
It landed on..5
would you like to play again? [y/n]y
Traceback (most recent call last):
  File "diceRoller.py", line 11, in <module>
    while (answer == "y" or "Y"):
NameError: name 'answer' is not defined
Damhan Richardson
  • 419
  • 1
  • 5
  • 16
  • 1
    [Short Description of Python Scoping Rules](http://stackoverflow.com/q/291978/2359271) – Air Oct 10 '14 at 21:44
  • 2
    `global answer` belongs *inside* the function. You'll have another problem after that, see [How do I test one variable against multiple values?](http://stackoverflow.com/q/15112125) – Martijn Pieters Oct 10 '14 at 21:44

4 Answers4

5

On top of what the answers are saying, I would advise you not to use global, instead I would return whether the person wanted to continue or not, and continue based off that, for example:

import random
input("Hit enter to roll the dice")
def rollDice():
    result = random.randrange(1,6)
    print("It landed on.. " + str(result))
    answer = input("Would you like to play again? [y/n]")
    if answer in ("y", "Y"):
        return True
    return False

while rollDice():
    continue

Also, use a loop instead of an if statement. Otherwise you won't be able to ask the user if he/she wants to continue indefinitely.

Dair
  • 15,910
  • 9
  • 62
  • 107
  • That is alot tidier.. thanks! however whats the purpose of returning false if we're returning true if they say Y for yes? – Damhan Richardson Oct 10 '14 at 21:57
  • Once you get to a return statement, you break out of the function. So if the user inputs "y" or "Y", you leave the function. The `return False` is to handle the other case, when they don't input "y" or "Y". – Dair Oct 10 '14 at 21:59
  • Or just `return (answer in ("y", "Y"))` – alexis Oct 11 '14 at 00:08
0

Your function definition is the thing that needs to know that answer is global. So put your global declaration inside the definition body.

def rollDice():
    global answer
    result = random.randrange(1,6)
    print ("It landed on.." + str(result))
    answer = input("would you like to play again? [y/n]")

Also

if (answer == "y" or "Y"):

should be

if answer in ('y', 'Y'):

otherwise you are checking if either (answer=="y") or ("Y"), the latter of which always evaluates to True.

If you want to keep rolling indefinitely as long as the users answers affirmatively, then your if should be a while.

while answer in ('y', 'Y'):
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

the global keyword has to be inside the function body. Also, like Falmarri said, if (answer=="y" or "Y") needs to be `if answer=="y" or answer=="Y"

clarkep
  • 795
  • 1
  • 6
  • 12
0

First of all, global answer has no effect in the global scope. Put it inside the function.

But it's pretty difficult to debug your code as the traceback is referring to a line of code that does not appear in the source you show! (while vs if)

In any case, if answer = 'y' or 'Y' should be if answer in tuple('yY')

jacg
  • 2,040
  • 1
  • 14
  • 27