Home > Enterprise >  Play game Rock Paper Scissors Lizard Spock in Python for 10 times not return different result
Play game Rock Paper Scissors Lizard Spock in Python for 10 times not return different result

Time:02-01

I'm working on Rock Paper Scissors Lizard Spock in Python game and I choose 2 players select random values Then I want to play it for many times and get the result, but it just played once and print the data many times

class Action(IntEnum):
    Rock = 0
    Paper = 1
    Scissors = 2
    Lizard = 3
    Spock = 4

victories = {
    Action.Scissors: [Action.Lizard, Action.Paper],
    Action.Paper: [Action.Spock, Action.Rock],
    Action.Rock: [Action.Lizard, Action.Scissors],
    Action.Lizard: [Action.Spock, Action.Paper],
    Action.Spock: [Action.Scissors, Action.Rock]
}

def get_user_selection():
    selection = random.randint(0, len(Action) - 1)
    action = Action(selection)
    return action

def get_computer_selection():
    selection = random.randint(0, len(Action) - 1)
    action = Action(selection)
    return action

user_action = get_user_selection()
computer_action = get_computer_selection()

def determine_winner(user_action, computer_action):
    defeats = victories[user_action]
    if user_action == computer_action:
        print(f"Both players selected {user_action.name}. It's a tie!")
    elif computer_action in defeats:
        print(f"{user_action.name} beats {computer_action.name}! You win!")
    else:
        print(f"{computer_action.name} beats {user_action.name}! You lose.")
res=[]
for n in range(5):
        new = determine_winner(user_action, computer_action)
        res.append(new)
        print(res)

Result is Spock beats Scissors! You lose. [None] Spock beats Scissors! You lose. [None, None] Spock beats Scissors! You lose. [None, None, None] Spock beats Scissors! You lose. [None, None, None, None] Spock beats Scissors! You lose. [None, None, None, None, None]

I want to have a different result every run

CodePudding user response:

You're only running get_user_selection() and get_computer_selection() once, then comparing them 5 times, move them into the for loop to repeat selections each time. Also to get the result you need to return a value from determine_winner() function, see below:

import pandas as pd
import random
from enum import IntEnum


class Action(IntEnum):
    Rock = 0
    Paper = 1
    Scissors = 2
    Lizard = 3
    Spock = 4

victories = {
    Action.Scissors: [Action.Lizard, Action.Paper],
    Action.Paper: [Action.Spock, Action.Rock],
    Action.Rock: [Action.Lizard, Action.Scissors],
    Action.Lizard: [Action.Spock, Action.Paper],
    Action.Spock: [Action.Scissors, Action.Rock]
}

def get_user_selection():
    selection = random.randint(0, len(Action) - 1)
    action = Action(selection)
    return action

def get_computer_selection():
    selection = random.randint(0, len(Action) - 1)
    action = Action(selection)
    return action



def determine_winner(user_action, computer_action):
    defeats = victories[user_action]
    if user_action == computer_action:
        print(f"Both players selected {user_action.name}. It's a tie!")
        return 'tie'
    elif computer_action in defeats:
        print(f"{user_action.name} beats {computer_action.name}! You win!")
        return 'win'
    else:
        print(f"{computer_action.name} beats {user_action.name}! You lose.")
        return 'lose'
res=[]
for n in range(5):
    user_action = get_user_selection()
    computer_action = get_computer_selection()
    new = determine_winner(user_action, computer_action)
    res.append(new)

outputs:

Paper beats Spock! You lose.
['lose']
Paper beats Rock! You win!
['lose', 'win']
Lizard beats Paper! You win!
['lose', 'win', 'win']
Paper beats Rock! You win!
['lose', 'win', 'win', 'win']
Scissors beats Paper! You lose.
['lose', 'win', 'win', 'win', 'lose']

  • Related