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