-1

I am a history student who was challenged to make this program so please assume I have little knowledge of coding.

I thought my program was approaching completion but am getting an error that the variables inside the roll function are not defined. However, when I run the roll function on its own there is no problem. I'm sure this is a simple mistake but, would like to know how to correct it, and why. (learning things is cool)

import random

def roll():
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)          
    r =  die1 + die2
return r

def Turn():
    pScore = 0
    cScore = 0
    turn = True
    while turn == True:
        pChoice = str(input("Would you like to roll? Type yes or no."))
        if pChoice == "yes":
            roll()
            if r == 2:
                turn = False
                pScore = 0
                print("You have rolled snake eyes. Your turn is over and         your score is 0.")
            elif die1 == 1 or die2 == 1:
                turn = False
                pScore += r
                print("You rolled {}, {} and your score is {}. your turn is     over".format(die1, die2, pScore))
            else :
                turn = True
                pScore += r
            print("You rolled {}, {} and your score is {}".format(die1, die2, pScore))
        if pChoice == "no":
            turn = False
            print("You have chosen not to roll. Your score is {}.".format(pScore))

    while turn == False:
        roll()
        if r == 2:
            turn = True
            cScore = 0
            print("The computer has rolled snake eyes. It's turn is over")
        elif die1 == 1 or die2 == 1:
            turn = true
            print("The computer {}, {} and its score is {}. Its turn is over".format(die1, die2, cScore))
        else:
            turn = False
            pScore =+ r
            print("The computer {}, {} and its score is {}.".format(die1, die2, cScore))

def main():
    pScore = 0
    cScore = 0
    while pScore <100 and cScore <100:
        Turn()
    if pScore >= 100:
        print("Your score is {} you win!".format(pScore))    
        return
    elif cScore >= 100:
        print("The computer score is {} you lose!".format(cScore))
        return
main()
Unlucky
  • 3
  • 1
  • In the Turn() always do r = roll() and you will be fine, because r inside roll() is a local variable. – Organis Feb 19 '17 at 22:09
  • Briefly: functions have their own scope, and returning a variable doesn't automatically save it to the calling scope. You have to save it, with e.g. `r = roll()`. – TigerhawkT3 Feb 19 '17 at 22:13

1 Answers1

0

In Turn, you reference r which is not in scope; see https://docs.python.org/3/reference/executionmodel.html. What you want appears to be something like r = roll() rather than just roll().

Some other things to be aware of:

  • For the same reason as above, pScore and cScore are never actually updated in main; you will want to return those values from Turn so as to be able to use them in main. In particular, the loop appearing in main is infinite.
  • while turn == True could be written simply as while turn. Similarly, while turn != True could be written as while not turn.
  • Letting turn = True when turn is guaranteed to be True does nothing.
  • The return statements in main do nothing.
fuglede
  • 17,388
  • 2
  • 54
  • 99
  • So, now that I have changed `roll()` to `r = roll()` this is pulling the value of `r` from `roll()` correct? I ask this because now that I have corrected my initial problem I am getting the same error for the dice. To pull the values for the dice would I simply type `die1 = roll()` – Unlucky Feb 19 '17 at 22:36
  • No, when you write `anything = roll()`, that `anything` becomes whatever you return from the function. That is, `die1 = roll()` would become what is called `r` in the implementation of `roll()`. In your case, what you might want to do is to look into having multiple return values from the `roll` function. – fuglede Feb 19 '17 at 22:39
  • That makes perfect sense and what I had initially thought. Thanks very much for your help. – Unlucky Feb 19 '17 at 22:41