Answer To: Design a python program that simulates the game rock, paper, and scissors. You are to keep track of...
Pratyayee answered on Aug 10 2021
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Game_RockPaperScissors.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fnaLHw9Z2sXB",
"outputId": "f8c38a1c-19bc-46e6-d733-7469ab040eb3"
},
"source": [
"import random\n",
"import math\n",
" \n",
"def play():\n",
" user = input(\"\\n\\n What's your choice? 'r' for rock, 'p' for paper, 's' for scissors\\n\")\n",
" user = user.lower()\n",
" \n",
" computer = random.choice(['r', 'p', 's'])\n",
" \n",
" if user == computer:\n",
" return (0, user, computer)\n",
" \n",
" # r > s, s > p, p > r\n",
" if is_win(user, computer):\n",
" return (1, user, computer)\n",
" \n",
" return (-1, user, computer)\n",
" \n",
"def is_win(player, opponent):\n",
" # return true is the player beats the opponent\n",
" # winning conditions: r > s, s > p, p > r\n",
" if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r'):\n",
" return True\n",
" return False\n",
" \n",
" \n",
"def play_best_of():\n",
" # play against the computer until player quits\n",
" \n",
" player_wins = 0\n",
" computer_wins = 0\n",
" tie = 0\n",
" \n",
" user1 = input(\"\\n Do you want to quit now? Type 'y' to quit, 'n' not to quit\\n\")\n",
" \n",
" \n",
" while user1 == 'n':\n",
" result, user, computer = play()\n",
" # tie\n",
" if result == 0:\n",
" tie += 1\n",
" print('It is a tie. You and the computer have both chosen {}. \\n\\n'.format(user))\n",
" print('No. of time player wins (current) : '+ str(player_wins))\n",
" print('No. of tie(current) : '+ str(tie))\n",
" print('No. of times the computer wins (current) : '+ str(computer_wins))\n",
" # you win\n",
" elif result == 1:\n",
" player_wins += 1\n",
" print('You chose {} and the computer chose {}. You won!\\n'.format(user, computer))\n",
" print('No. of time player wins (current) : '+ str(player_wins))\n",
" print('No. of...