Python implements guessing game

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

**1. Problem description: **

Use python to develop a small number guessing game. The program randomly generates a number between 0 and 1024. The user enters the guessed number, and the program tells the user whether the guess is big or small. The user who guesses right within a certain number of times wins, otherwise the user loses.
Each round of the game requires users to enter a username
The program will continue to run until the user enters "3" to stop the game. Enter "1" before each round of the game to view user input history.

**2. Knowledge points involved: **

1、 Randomly generate numbers, design random number module of python.
2、 The user enters a number and the program outputs the result. Involving python input and output modules.
3、 The program automatically proceeds to the next round, involving the python loop module.
4、 Judging user input involves the p ython condition judgment module.
5、 Query user input history, involving python dictionary and list modules.

Three, code construction

# Call the random number generation module
import random 
# Start the game control module
def start():
 name =input('Please enter your name:')if name =='drop out':returnif name not in history:
 history[name]=[]
 answer = random.randint(0,1024)try_to_guess(name, answer)
 
def try_to_guess(name ,answer): #Guess the main module of the game
 try_nume =0while try_nume <100:
 guess_answer =int(input('Please enter a number you guessed:'))if guess_answer < answer :print('The number you entered is less than the correct answer')
 elif guess_answer   answer:print('The number you entered is greater than the correct answer')else:print('Congratulations on your correct answer!')
 history[name].append('success!')break
 try_nume +=1else:print('Too many wrong guesses, the game fails!')
 history[name].append('failure!')

# History module
history ={}#Create collection, store history
def show_history():for name, data in history.items():print('user:{},The record is as follows:{}'.format(name, data))
# Prevent input options that are not available
def default():print('input error! please enter again')print('='*40)if __name__ =='__main__':
 select_dict ={'1': start,'2': show_history,'3': exit}while True:
 select =input('1.Start the game\n2.history record\n3.exit the game\nPlease enter the number to choose:')
 select_dict.get(select,default)()

Four, debugging and running

Regardless of success or failure, press 1 to start the game, press 2 to view the record, press 3 to exit the game, and enter other characters to report an error.

For more wonderful articles about python games, please click to view the following topics:

python tetris game collection

Python classic games summary

Python WeChat Jump Jump Game Collection

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

Recommended Posts

Python implements guessing game
Python implements word guessing game
Python implements a 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 implements simple tic-tac-toe game
Python implements the brick-and-mortar game
Python simply implements the snake game
Python implements digital bomb game program
Python implements Super Mario
Python, PyGame game project
Python implements image stitching
Python implements the source code of the snake game
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements simple tank battle
Python3 realizes airplane war game
Python implements udp chat window
python guess the word game
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 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
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 implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down
Python implements QQ mailbox to send mail
Python implements alternate execution of two threads
Python solves the Tower of Hanoi game
Python implements the sum of fractional sequences