Hi, I have been learning Python for a while slowly making my way through Learn Python the Hard way. I stopped coding for a while, but I wanted to make sure I remembered everything I had already learned by building a game of Pig before I move on.
I seem to have the basic logic of the game down, but for some reason it won't change turn. I have gone over my logic multiple times, but it seems right. I am also getting some small bugs if you play long enough, but perhaps once I get my change turn function working correctly it will straiten itself out.
Can someone help me figure out what's wrong?
Here's my code.
import random
def roll(die, turnTotal, currentPlayer, playerList):
"""create the random counter or die and output the result to the screen, and check if the die roll is 1, if so set turn total to 0 and pass the player variable to the next player."""
die = random.randrange(1,7)
if die == 1:
turnTotal = 0
changeTurn(currentPlayer, playerList)
print("Pig!!! \nYou rolled a 1. It is now the other players turn")
else:
turnTotal[0] += die
print("You rolled a %s" % die)
print("your total for this turn is \n%s." % turnTotal[0])
def changeTurn(currentPlayer, playerList):
"""change turn between player 1 and player 2."""
if currentPlayer[0] == playerList[0][0]:
return playerList[1]
else:
return playerList[0]
def game():
player1 = ["Player1", 0]
player2 = ["Player2", 0]
playerList = [player1, player2]
currentPlayer = random.choice(playerList)
turnTotal = [0]
winner = None
die = 0
print("Pig\n")
while not winner:
if currentPlayer[1] < 100:
print("It is %s\'s turn.\n" % currentPlayer[0])
print("%s\'s score is %s, and %s\'s score is %s." % (playerList[0][0], playerList[0][1], playerList[1][0], playerList[1][1]))
rollDice = input("press enter to roll, or hold/h to keep your current total: ")
if rollDice == "":
roll(die, turnTotal, currentPlayer, playerList)
elif rollDice == "hold" or rollDice == "h":
currentPlayer[1] += turnTotal[0]
turnTotal[0] -= turnTotal[0]
changeTurn(currentPlayer, playerList)
else:
winner = currentPlayer
print("Congradulations! %s won!" % winner[0])
def GameDescription():
"""outputs a descriptionof the game Pig, provides an input box, so that when the the user presses enter the game will return to the main menu"""
print("""Pig is a two player game where one player rolls one die as many times as he or \nshe wants. However, if the player rolls a 1 their score for that turn is 0, and it becomes the other player\'s turn. Each time you roll the die the face value of the die adds to your turn total and if you decide you have taken enough of a \nwrisk for that turn, type \"hold\" or \"h.\" This will add the turn total to your \noverall score. The objective is to be the first person to reach 100.""")
input("Press enter to return to the main menu. ")
main()
def main():
"""The main menu of the game. provides the user with 3 options, play, rules, or exit."""
while True:
print("Welcome to the dice game Pig.")
mainMenu = input("1 to play \n2 to learn about the rules for the game \n3 to exit: ")
if mainMenu == "1":
game()
elif mainMenu == "2":
gameDescription()
elif mainMenu == "3":
break
main()