Python implements a guessing game

The examples in this article share the specific code for implementing a guessing game in python for your reference. The specific content is as follows

To design a guessing game, the requirements are as follows:

1、 When the game starts, enter the player name
2、 You can choose 3 computer opponents (computer name is customized)
3、 The rules of the game are: 1, scissors 2, rock 3, cloth
4、 At the beginning of the game, the player vs. the computer, the player chooses one, the computer is randomly 1-3
5、 With statistics on the number of rounds (such as: which round is it now), statistics on winning and losing (how many rounds the player wins, how many rounds the computer wins)

The renderings are as follows:

code show as below:

import random #Import random numbers first
classgame():   #The class for creating a game is divided into two attributes, one player name and one computer name
 def __init__(self,playername,computername):
 self.playername=playername
 self.computername=computername
 self.playerscore=0 #Player score
 self.comscore=0  #Computer score
 self.sum=0   #Number of draws
 def startgame(self):
 newname=input("Please enter your name:")
 self.playername=newname
 dnname=input("Please choose your opponent: 1.Wang Yuan, 2.Wang Junkai, 3.Yi Yang Qianxi:")if dnname=="1":print("Your opponent is Wang Yuan")
 self.computername="Wang Yuan"
 elif dnname=="2":print("Your opponent is Wang Junkai")
 self.computername ="Wang Junkai"
 elif dnname=="3":print("Your opponent is Yi Yang Qianxi")
 self.computername ="Yi Yang Qianxi"else:print("The input is wrong, the system thought you picked Wang Junkai randomly")
 self.computername ="Wang Junkai"print("--------The guessing game starts,%s VS %s--------"%(self.playername, self.computername))print("The rules of the game are: 1.Scissors, 2.Stone, 3.cloth")while True:
 player=int(input("Players please punch:"))
 # Player punches
 player = random.randint(1,3)if player ==1:print("  %s out of the scissors"% self.playername)
 elif player ==2:print("  %s out of the stone"% self.playername)else:print("  %s out of cloth"% self.playername)
 # Computer punches
 computer=random.randint(1,3)if computer==1:print("  %s out of the scissors"%self.computername)
 elif computer==2:print("  %s out of the stone"%self.computername)else:print("  %s out of cloth"%self.computername)if(player==computer):print("draw")
 self.sum+=1elif(player==1 and computer==3)or(player==2 and computer==1)or(player==3 and computer==2):print("%s win this game"%self.playername)
 self.playerscore+=1else:print("%s win this game"%self.computername)
 self.comscore+=1print("Winning or losing statistics:%s VS %s"%(self.playername, self.computername),"Player%s victory%d times"%(self.playername, self.playerscore),"%s victory%d times"%(self.computername, self.comscore),"%d times平局"% self.sum)print("---------------------------------------------------")
 tag =input("Whether to continue y/n :")if tag.lower()=='n':print("Winning or losing statistics:%s VS %s"%(self.playername, self.computername),"Player%s victory%d times"%(self.playername, self.playerscore),"%s victory%d times"%(self.computername, self.comscore),"%d times平局"% self.sum)break

lx=game("as","hfg") #The number of parameters passed in must be equal to the number of defined parameters
lx.startgame()  #Call the method to start the game

More interesting classic mini game implementation topics, share with you:

C++ classic games summary

Python classic games summary

python tetris game collection

JavaScript classic games are constantly playing

Summary of classic java games

JavaScript classic games summary

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements a guessing game
Python implements guessing game
Python implements word guessing game
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements Tetris game
Python implements minesweeper game
Python implements WeChat airplane game
Python implements digital bomb game
Python realizes the guessing game
Python implements simple tic-tac-toe game
Python implements the brick-and-mortar game
Python simply implements the snake game
Python implements a universal web framework
Python implements digital bomb game program
How to simulate gravity in a Python game
Python implements the source code of the snake game
Python implements a simple business card management system
Python implements man-machine gobang
Python, PyGame game project
Python implements image stitching
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements FTP to upload files in a loop
Python defines a function method
Python implements simple tank battle
Python3 realizes airplane war game
Python implements udp chat window
Python implements parking management system
Python realizes apple eating game
Python implements TCP file transfer
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Why call python a glue language
Python implements student performance evaluation system
How Python implements the mail function
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python is a cross-platform language code
Python implements 126 mailboxes to send mail
A summary of 200 Python standard libraries!
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Can python become a mainstream language?