#Christian Chiang #Girls Who Code Application import random def choose(): tool = "q" while tool != "r" and tool != "p" and tool != "s": print("Pick rock (R), paper (P), or scissors (S)") tool = input().lower() return tool #The first few (3) games are random by the CPU. #Afterwards, the CPU takes in previous inputs and tries to learn from them. def cpuStrat(list1, options): if len(list1) <=3: cpuMove = random.choice(options) else: oldMove = random.choice(list1) if oldMove == 'p': cpuMove = 's' elif oldMove == 's': cpuMove = 'r' elif oldMove == 'r': cpuMove = 'p' return cpuMove def winLose(man, cpu): if man == 'r' and cpu == 's': win = 2 elif man == 's' and cpu == 'p': win = 2 elif man == 'p' and cpu == 'r': win = 2 elif cpu == 'r' and man == 's': win = 0 elif cpu == 's' and man == 'p': win = 0 elif cpu == 'p' and man == 'r': win = 0 else: win = 1 return win def aesthetic(choice): if choice == 'r': extended = "rock" elif choice == 's': extended = "scissors" elif choice == 'p': extended = "paper" return extended def playAgain(): print("Do you want to play again? (yes or no)") return input().lower().startswith("y") def main(): print("Welcome to Rock Paper Scissors!") #globals movesList = [] rps = ['r', 'p', 's'] dubs = 0 ls = 0 ties = 0 loop = True while loop: playerMove = choose() movesList.append(playerMove) cpuMove = cpuStrat(movesList, rps) print("You chose:", aesthetic(playerMove)) print("The CPU chose:", aesthetic(cpuMove)) result = winLose(playerMove, cpuMove) if result == 2: dubs += 1 print("You won!") elif result == 1: ties += 1 print("You tied!") elif result == 0: ls += 1 print("You lost!") if not playAgain(): print("Your Stats\nWins:", dubs,"Losses:", ls, "Ties:", ties,"\nGoodbye.") break main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question