diff --git a/BrowserHistory/rock_paper_scissors.py b/BrowserHistory/rock_paper_scissors.py new file mode 100644 index 00000000000..c6fb00102d6 --- /dev/null +++ b/BrowserHistory/rock_paper_scissors.py @@ -0,0 +1,48 @@ +""" +Rock, Paper, Scissors Game (CLI Version) +Author: Your Name +""" + +import random + + +def get_user_choice(): + """Prompt the user to enter their choice.""" + choice = input("Enter your choice (rock, paper, scissors): ").lower() + if choice in ["rock", "paper", "scissors"]: + return choice + else: + print("Invalid choice! Please enter rock, paper, or scissors.") + return get_user_choice() + + +def get_computer_choice(): + """Randomly select computer's choice.""" + options = ["rock", "paper", "scissors"] + return random.choice(options) + + +def decide_winner(player, computer): + """Decide the winner based on the choices.""" + if player == computer: + return "It's a draw!" + elif ( + (player == "rock" and computer == "scissors") + or (player == "paper" and computer == "rock") + or (player == "scissors" and computer == "paper") + ): + return "You win!" + else: + return "Computer wins!" + + +def main(): + """Main function to play the game.""" + user_choice = get_user_choice() + computer_choice = get_computer_choice() + print(f"Computer chose: {computer_choice}") + print(decide_winner(user_choice, computer_choice)) + + +if __name__ == "__main__": + main() diff --git a/rock_paper_scissor_game.py b/rock_paper_scissor_game.py deleted file mode 100644 index 1cc3c8dedd3..00000000000 --- a/rock_paper_scissor_game.py +++ /dev/null @@ -1,52 +0,0 @@ -from __future__ import print_function - -import random - - -# let -# 0 - rock -# 1 - paper -# 2 - scissor - - -def name_to_number(name): - if name == "rock": - name = 0 - elif name == "paper": - name = 1 - elif name == "scissors": - name = 2 - return name - - -def number_to_name(number): - if number == 0: - return "rock" - elif number == 1: - return "paper" - elif number == 2: - return "scissors" - - -def game(player_choice): - print() - name = player_choice - print(name) - number = name_to_number(name) - comp_number = random.randrange(0, 2) - comp_choice = number_to_name(comp_number) - print(comp_choice) - - comp = -int(comp_number) - play = int(number) - diff = (comp + play) % 5 - - if diff == 1 or diff == 3: - print("you won!!!") - elif diff == 0: - print("draw") - elif diff == 2 or diff == 4: - print("you lose!!!") - - -# Also improve it diff --git a/rock_paper_scissors.py b/rock_paper_scissors.py new file mode 100644 index 00000000000..e34264d1cfc --- /dev/null +++ b/rock_paper_scissors.py @@ -0,0 +1,48 @@ +""" +Rock, Paper, Scissors Game +Author: DEVANSH-GAJJAR +""" + +import random + + +def get_user_choice(): + """Prompt the user to enter their choice.""" + choice = input("Enter your choice (rock, paper, scissors): ").lower() + if choice in ["rock", "paper", "scissors"]: + return choice + else: + print("Invalid choice! Please enter rock, paper, or scissors.") + return get_user_choice() + + +def get_computer_choice(): + """Randomly select computer's choice.""" + options = ["rock", "paper", "scissors"] + return random.choice(options) + + +def decide_winner(player, computer): + """Decide the winner based on the choices.""" + if player == computer: + return "It's a draw!" + elif ( + (player == "rock" and computer == "scissors") + or (player == "paper" and computer == "rock") + or (player == "scissors" and computer == "paper") + ): + return "You win!" + else: + return "Computer wins!" + + +def main(): + """Main function to play the game.""" + user_choice = get_user_choice() + computer_choice = get_computer_choice() + print(f"Computer chose: {computer_choice}") + print(decide_winner(user_choice, computer_choice)) + + +if __name__ == "__main__": + main()