# My first attempt at a simple game. Any feedback welcome :) import random # Input rock, paper or scissors player = input() # AI choice choices = ("rock", "paper", "scissors") # Obviously some of what im going to say dont really apply to such a small example but they are generally good things to consider. # Code should be DRY (Don't repeat yourself) if you set the win or loose response as a method you can call it each time, plus if you ever have to change the response you only need to do it in one place class Choice: Rock = choices[0] Paper = choices[1] Scissors = choices[2] class Result: Win = "win" Lose = "lose" Draw = "draw" def result_message(result): if result == Result.Win: print("You Win!!!!") elif result == Result.Lose: print("You Lose!!!") elif result == Result.Draw: print("It's a Draw!!!") return "Something went wrong!" computer = random.choice(choices) # Game logic # Prepare for what a user submits they could throw in anything - caps + lowercase # Convert player to lowercase player = player.lower() # Check for draw if player == computer: result_message(Result.Draw) # Check rock elif player == Choice.Rock: if computer == Choice.Paper: result_message(Result.Lose) else: result_message(Result.Win) # Check for paper elif player == Choice.Paper: if computer == Choice.Scissors: result_message(Result.Lose) else: result_message(Result.Win) # check scissors elif player == Choice.Scissors: if computer == Choice.Rock: result_message(Result.Lose) else: result_message(Result.Win) # check if a choice else: print("That's not a valid play. Check your spelling!") #print choices print(player, computer)
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