Note: This code was first posted on another forum by me under the name Candy. Read this note before you declare this code copied.
Since I was bored, I decided to code a Rock Paper Scissors game in Python in my iOS device (There are offline interpreters available). The very first version of my code was ugly and large, so I decided to re-write the whole thing from scratch. I have tried to keep the code short because coding in mobile was a bit difficult.
I would like to know your suggestions for improvement of this game. Also point out any bad/wrong practices that you find. I don't even know whether I follow the right way of naming variables. So please tell me the 'correct' naming conventions in Python. And yeah, try not to be too harsh on this one. After all, it's my first game in Python :)
import random
import os
pad = "|| "
def GetInput():
print(pad + "(1)Rock, (2)Paper or (3)Scissor?")
sInput = raw_input(pad + "User: ").lower()
if sInput in ["rock", "r", "1"]:
return 1
elif sInput in ["paper", "l", "2"]:
return 2
elif sInput in ["scissor", "s", "3"]:
return 3
else:
return -1
def GetResult(pA, pB):
res = (3 + pA - pB) % 3
if res == 1:
return "win"
elif res == 2:
return "lose"
elif res == 0:
return "draw"
pass
def GetAIOutput():
out = random.randint(1, 3)
return out
Stats = { "Win" : 0, "Lose" : 0, "Draw" : 0, "Total" : 0 }
def UpdateStats(state):
Stats["Total"] += 1
if state == "win":
Stats["Win"] += 1
elif state == "lose":
Stats["Lose"] += 1
else:
Stats["Draw"] += 1
def DisplayStats():
print("///////Stats//////////////////////////")
for key, value in Stats.items():
print(pad + key + " : " + str(value))
print(pad)
def DisplayIntro():
print( "=======================================")
print(pad + " Rock, Paper, Scissors")
print(pad + " by CandyV3rm")
print( "=======================================")
print( "#######Game############################")
def DisplayOutro():
print(pad + "\n" + pad + "End of current game")
print("==============================\n\n\n")
def ClearScreen():
os.system('cls' if os.name=='nt' else 'clear')
def StartRPSGame():
bAgain = True
# Repeat if user wants to play again
while bAgain:
# Clear the screen
ClearScreen()
# Display the intro
DisplayIntro()
# Get user input
uInput = GetInput()
# Handle invalid input
if uInput < 1:
print(pad + "Invalid Input")
DisplayOutro()
continue
# Get AI output
eOutput = GetAIOutput()
# Display AI output
print(pad + "Computer: " + str(eOutput))
# Determine result - win, lose or draw
result = GetResult(uInput, eOutput)
# Display results
print(pad + "Result = " + result + "\n" + pad)
# Update the scores
UpdateStats(result)
# Display the scores
DisplayStats()
# Ask if user wants to play again
print("==============================")
print(pad + "Play again? (yes=1;no=0)")
bAgain = bool(input(pad + "Enter: "))
# Display the outro
DisplayOutro()
raw_input("Press enter.")
# Start Rock, Paper Scissors game
StartRPSGame()
Edit: Me and my friend are challenging each other on who will code a better version. Here's his version in c++: Rock, Paper, Scissor Game
lowercase_with_underscores
, which the exception of classes, which areCamelCase
. The Python style guide is PEP 8. \$\endgroup\$