SoFunction
Updated on 2024-10-29

Sample code for python to realize human and computer guessing.

Sample code for python to realize human and computer guessing.

Updated on March 02, 2020 10:31:15 by Lone Cold rm-
This article introduces the python implementation of human and computer guessing sample code, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, the need for friends below with the editorial to learn together!

Complete the development of an interactive human-computer guessing game, where the user implements the punches through console inputs and the computer implements the punches through random numbers in the program, and outputs the results at the end of each round. The game is stopped when the user inputs n and the total result is output.

import random

all = ['Stone','Scissors','Boo']
computer = (['Stone','Scissors','Boo'])

#All winnings
win = [['Stone','Scissors'],['Boo','Stone'],['Scissors','Boo']]

class Text():

 def func_play(self):
  ind = input('Please enter [0] rock [1] scissors [2] cloth')
  if ():
   try:
    raise ValueError('Please enter a number')
   except ValueError as v:
    print(v)
  elif ():
   ind = int(ind)
   if 0<=ind<=2:
    play = all[ind]
    print('You type %s, the computer types %s'%(play,computer))
    if play == computer:
      = 'Tie game'
    elif [play, computer] in win:
      = 'You win'
    else:
      = 'You lost'
   else:
    print('Please enter a number between 0 and 2')
   print()
 def write_file(self):
  with open('','a',encoding='utf-8') as f:
   (+'\n')
while True:
 t = Text()
 t.func_play()
 t.write_file()

to this article on the python implementation of human and computer guessing sample code is introduced to this article, more related python human and computer guessing content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!

  • python
  • rock-paper-scissors game

Related articles

  • Python read data in excel file, plot line graphs and scatter plots

    This article introduces the Python read excel file data, drawing line graphs and scatterplots, the article around the theme to develop a detailed introduction, with certain reference value, the need for partners can refer to it!
    2022-09-09
  • python implementation of grammatical left-recursive elimination

    This article introduces python to achieve the elimination of grammatical left-recursive method, this article through the example code to give you a very detailed, for everyone to learn or work with a certain reference value, the need for friends can refer to the next!
    2020-05-05
  • Python implementation of counting and bucket sort example code

    This article introduces the python counting sorting and bucket sorting, the text through the sample code is very detailed, for everyone's learning or work with certain reference learning value, the need for friends below with the editor to learn together!
    2019-03-03
  • Usage of Python closures

    This article mainly introduces the use of Python closures, when the return of the internal function uses the variables of the external function on the formation of closures, the following more related to the content of the need for partners can refer to it
    2022-04-04
  • pyqt5 login interface using pyqtDesigner

    This article is mainly for you to introduce in detail the pyqt5 use pyqtDesigner to realize the login interface, with certain reference value, interested partners can refer to it!
    2019-03-03
  • Ways to plot and output an image to a file using Python Matplotlib

    This article introduces the use of Python Matplotlib plotting and output images to the file in the way, with good reference value, I hope you can help, if there is any error or not fully consider the place, I hope not to give advice!
    2023-09-09
  • Example analysis of local variable usage in python functions

    This article introduces the python function of local variable usage, a more detailed analysis of the principle of Python local variables and the use of skills, and a comparative analysis of the local variables and global global variable usage differences, with certain reference value, you can refer to the following
    2015-08-08
  • Pandas method of counting values inside duplicate columns

    Today, I'd like to share a Pandas statistics duplicate the value of the column inside the method, has a good reference value, I hope to help you. Together follow the editor over to see it
    2019-01-01
  • Python based implementation of flipping images using Pygame

    This article introduces the use of Python based Pygame to flip the image, we will learn how to use Pygame to flip the image, to flip the image, we need to use (Surface, xbool, ybool) method, the method is called according to our needs in the vertical or horizontal direction of the image to flip the image, the following to look at the specific implementation of the process of it!
    2022-02-02
  • Installing PaddlePaddle locallyInstallation guide and step-by-step details

    PaddlePaddle is an open source and open deep learning platform developed by Baidu, with a comprehensive range of officially supported industrial-grade application models, covering a variety of areas such as natural language processing, computer vision, recommendation engines, and open a number of leading pre-trained Chinese models. This article introduces the local installation of PaddlePaddle installation guide, friends who need it can refer to the following
    2021-12-12

Latest Comments