Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python game example


import random
import time

computerwins = 0
playerwins = 0
ties = 0
end = 0

while True:

    choices = ["rock",
               "paper",
               "scissors"]

    userChoice = raw_input("Rock, Paper, Scissors, or End")

    computerChoice = (random.choice(choices))
    print(computerChoice)

    if userChoice == computerChoice:
        time.sleep(0.5)
        print("Tie!
")
        ties += 1
        end += 1

    elif userChoice == "rock":
        if computerChoice == "paper":
            time.sleep(0.5)
            print("Computer Win!
")
            computerwins +=1
            end += 1

        else:
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

    elif userChoice == "paper":
        if computerChoice == "rock":
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

        else:
            time.sleep(0.5)
            print("Computer win!
")
            computerwins += 1
            end += 1

    elif userChoice == "scissors":
        if computerChoice == "rock":
            time.sleep(0.5)
            print("Computer win!
")
            computerwins += 1
            end += 1

        else:
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

    elif userChoice == "end":
            choices.append("end")
            print("
Great game!
")
            print("Total score for computer: ", computerwins, "wins!")
            print("Total score for player: ", playerwins, "wins!")
            print("Total ties: ", ties, "ties!")
            time.sleep(2)
            break
Source by opensource.com #
 
PREVIOUS NEXT
Tagged: #python #game
ADD COMMENT
Topic
Name
9+7 =