+{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNvNZkVif8ko7fMs4sK+SAt"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **PYTHON PROJECT : ROCK-PAPER-SCISSOR**\n","---\n","## Write a program that allows the user to play a game of Rock Paper Scissors against the computer. The program should prompt the user to enter their choice (Rock, Paper, or Scissors) and then randomly generate a choice for the computer. The program should then determine the winner based on the following rules:\n","\n","- ## Rock beats Scissors\n","- ## Scissors beats Paper\n","- ## Paper beats Rock\n","---\n","## **Importing module 'random' :**\n","\n","- ### 'random' is a python module that generates random variables (random integers, bytes, sequences) based on the input that we give.\n","- ### In Rock Paper Scissors, as we randomly choose any one of the three, so we need to import random for that.\n","---\n","## **Defining our choices :**\n","- ### Creating a list of our choices."],"metadata":{"id":"nzo973D7-QSa"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"SkhVmold9E8W"},"outputs":[],"source":["import random\n","choices = ['rock','paper','scissor']"]},{"cell_type":"markdown","source":["### Now the logic to do this is : we have to enter our choice and the computer will randomly select one of these 3 choices > then we will define our rule, like - if 'scissor' > 'paper' > 'rock'.\n","## **Defining a function for computer's random choice :**\n","---\n","- ### **'random.choice'** - is a function present in module 'random' that randomly chooses one of the choices that we give from the list 'choices'.\n"],"metadata":{"id":"BKKy2tDBAemJ"}},{"cell_type":"code","source":["def get_computer_choice() :\n"," return random.choice(choices)"],"metadata":{"id":"mvWysUa9Aabd"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Defining a user function :**\n","---\n","- ### Asking user to enter a choice and using **lower()** function convert the input strings into lower vase as our list 'choices' are all in lower case.\n","- ### Now if the user input present in the list 'choices', then the function will return the user choice."],"metadata":{"id":"z9voHCiyCkNG"}},{"cell_type":"code","source":["def get_user_choice() :\n"," user_choice = input(\"Enter your choice, Rock paper or scissor? \").lower()\n"," if user_choice in choices :\n"," return user_choice\n"," else :\n"," return \"Invalid choice. Please try again.\""],"metadata":{"id":"t6A9ZoxSCjgk"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Applying game logic to determine the winner :**\n","---\n","- ### Defining function **'determine_winner( )'** below which is dependent on two parameters - user choice and computer choice.\n","- ### Now the variables '***user_choice***' and '***computer_choice***' are local variables which only apply for the function itself. So within the function we have to define what each of these variables stands for.\n","## **Let's define the logic within the function 'winner' :**\n","---\n","- ### If both user and computer choices are same - then it will be a tie.\n","- ### Then within **'elif'** statement we will defines all the instances where the user can win.\n","- ### Otherwise the computer wins."],"metadata":{"id":"TUq0eU6SE31h"}},{"cell_type":"code","source":["def determine_winner(user_choice, computer_choice) :\n","\n"," if user_choice == computer_choice :\n"," return \"It's a tie!\"\n","\n"," elif (user_choice == 'rock' and computer_choice == 'scissor') or \\\n"," (user_choice == 'scissor' and computer_choice == 'paper') or \\\n"," (user_choice == 'paper' and computer_choice == 'rock') :\n"," return \"You win!\"\n","\n"," else :\n"," return \"Computer wins!\""],"metadata":{"id":"qP-7M1NCEnAB"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Defining our game function - 'game_play' :**\n","---\n","- ### Now **' while loop'** keeps running until the user manually stops the program.\n","- ### Defining local variables **'user_choice'** and **'computer_value'** which will call the two functions - **'get_user_choice()'** and **'get_computer_choice()'** which we have defined earlier and the output will be assigned to them."],"metadata":{"id":"ec4dSKXhQ08z"}},{"cell_type":"code","source":["def game_play() :\n"," print(\"Let's play!\")\n","\n"," while True :\n"," user_choice = get_user_choice()\n"," computer_value = get_computer_choice()\n"," print(f\"You chose : {user_choice}\") # It will print the user and computer choice\n"," print(f\"Computer chose : {computer_value}\")\n"," result = determine_winner(user_choice, computer_value) # Call function 'winner' to determine the winner\n"," print(result)\n","\n"," play_again = input(\"do you want to play again? (y/n) : \").lower()\n"," if play_again != \"y\" : # If the user don't want to play again, then the loop breaks\n"," break"],"metadata":{"id":"egjoQNBWQPsj"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Let's run the function :"],"metadata":{"id":"8OwK6hYk_t0L"}},{"cell_type":"code","source":["game_play()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"63SvIx_gqtRW","outputId":"877cb26d-727e-4605-fff8-e1ed25f121a7","executionInfo":{"status":"ok","timestamp":1727012550298,"user_tz":-330,"elapsed":14760,"user":{"displayName":"Anindya Chatterjee","userId":"07568261938821269796"}}},"execution_count":null,"outputs":[{"name":"stdout","output_type":"stream","text":["Let's play!\n","Enter your choice, Rock paper or scissor? rock\n","You chose : rock\n","Computer chose : scissor\n","You win!\n","do you want to play again? (y/n) : y\n","Enter your choice, Rock paper or scissor? paper\n","You chose : paper\n","Computer chose : paper\n","It's a tie!\n","do you want to play again? (y/n) : n\n"]}]}]}
0 commit comments