Skip to content

Instantly share code, notes, and snippets.

@NoraCodes
Created February 14, 2016 02:06
Show Gist options
  • Save NoraCodes/240fa6580b5591018e19 to your computer and use it in GitHub Desktop.
Save NoraCodes/240fa6580b5591018e19 to your computer and use it in GitHub Desktop.
A game of pig, in Python 3
import random
command = ""
turns = 0
totalScore = 0
turnScore = 0
while totalScore < 25:
print("Turn: {} Score: {} in {} turns.".format(turnScore, totalScore, turns))
command = input("command> ").lower()
if command == "roll":
roll = random.randint(1,6)
if roll == 1:
print("You rolled a 1! Poor you.")
turnScore = 0
turns = turns + 1
else:
#REMOVED print
turnScore = turnScore + roll
elif command == "hold":
print("You've ended your turn.")
totalScore = totalScore + turnScore
turnScore = 0
turns = turns + 1
elif command == "quit":
print("Bye!")
break;
else:
print("What?")
print("FINAL SCORE: {} in {} turns ({})".format(totalScore, turns, totalScore/turns))
# Added for writing the score
if totalScore > 25:
name = input("Winner! Enter your name: ")
with open("scoreboard.txt", "w") as f:
f.write("{}//{}//{}\n".format(name, turns, totalScore))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment