front , We talked about some kinds of variables and strings . Friends , Do you remember the word game before us ? today , The first thing we need to do is to improve our word games , As for the second thing , Let me sell you a pass first , Dear guests , Pay attention !

1) first , According to feedback from some users , There are three main deficiencies in our game :
① When you guess wrong, the program should have a prompt ( For example, tell users that the number is bigger or smaller )
② Every time the game is executed , Users can only guess once , There should be multiple opportunities for users to guess
③ The answer is the same every time the game runs , It is easy to lead to the leakage of the answer , So we need to set the answer to be random

therefore , We according to these three requirements to achieve a more humanized text game !

The first step :
To achieve this requirement , Let's introduce the concept of conditional branching :
This basic module is :

if condition :
indent The action to perform when the condition is true
else:
indent Statement executed when the condition is false

So the code after implementation is :
print('--------- Welcome to the glory of the king ---------') guess = input(' Please enter the number you want to enter :') temp = int(
guess) if temp == 8: print(' congratulations ! Right ') print(' game over !') else: if temp > 8: print(
' Brother , Big, big ~~') else: print(' Well , It's small. It's small ~') print(' game over !')
This basically fulfilled our first requirement : When playing the game can prompt guess big or small information

Now let's take a look at the second requirement , To achieve more than one game experience , What should I do with this ? Don't panic , We Python Yes while The cyclic structure of :
This basic module is :

while condition :
indent Statement executed when the condition is true

Now let's take a look at our code :
print('--------- Welcome to the glory of the king ---------') temp = int(input(' Please enter the number you want to enter :')) while
temp!= 8: if temp > 8: print(' Brother , Big, big ~~') else: print(' Well , It's small. It's small ~') temp = int(input
(' Please re-enter a number :')) if temp == 8: print(' congratulations ! Right , game over !') else: if temp > 8: print(
' Brother , Big, big ~~') else: print(' Well , It's small. It's small ~') print(' game over !')
okay , That's all , Let's meet the third requirement :
Here we introduce a random function module :random, This is also a common one BIF, It has one in it randint function , Is used to generate random numbers :
import random print('--------- Welcome to the glory of the king ---------') temp = int(input(
' Please enter the number you want to enter :')) reality = random.randint(1,10) if temp > reality: print(
' Brother , Big, big ~~') elif temp < reality: print(' Well , It's small. It's small ~') else: print(' congratulations ! bingo !') while
temp!= reality: temp = int(input(' Please re-enter a number :')) if temp == reality: print(
' congratulations ! Right , game over !') else: if temp > reality: print(' Brother , It's big ~~') else: print(
' Well , It's small. It's small ~') print(' game over !')
of course , We can be more humane :
① We can set the upper limit for wrong answers , Over that limit , Users can't play anymore
② We can set the interface to reappear at intervals —— Not as soon as we've typed the information , The next sentence has already appeared , We can make the game interactive

For the first point , Let's take three times as an example ( here , We still have to use it while loop ):
times Represents the number of times
The code is as follows :
import random print('--------- Welcome to the glory of the king ---------') temp = int(input(
' Please enter the number you want to enter :')) times = 3 reality = random.randint(1,10) if temp > reality:
print(' Brother , Big, big ~~') elif temp < reality: print(' Well , It's small. It's small ~') else: print(' congratulations ! bingo !')
times-= 1 while temp != reality: while times > 0: temp = int(input(' Please re-enter a number :')
) if temp == reality: print(' congratulations ! Right , game over !') times -= 1 else: if temp > reality:
print(' Brother , Big, big ~~') times -= 1 else: print(' Well , It's small. It's small ~') times -= 1 else: print(
' Your times have run out !') break print(' game over !')
As for the second point , We need to introduce a new one BIF——time function
The code is as follows :
import random import time print('--------- Welcome to the glory of the king ---------') time.sleep(1)
temp= int(input(' Please enter the number you want to enter :')) times = 3 reality = random.randint(1,10) if
temp> reality: print(' Brother , Big, big ~~') elif temp < reality: print(' Well , It's small. It's small ~') else:
print(' congratulations ! bingo !') times -= 1 while temp != reality: while times > 0: temp = int(
input(' Please re-enter a number :')) if temp == reality: print(' congratulations ! Right , game over !') times -= 1 else:
if temp > reality: print(' Brother , Big, big ~~') times -= 1 else: print(' Well , It's small. It's small ~') times -=
1 else: print(' Your times have run out !') break time.sleep(1.2) print(' game over !')
among ,time.sleep() You can fill in both floating-point numbers and integers in the following brackets .
in addition , that random.randint(1,10) include 1 But not included 10

okay , Come here , You've basically mastered it Python The most basic knowledge , well , See you next time !

Technology
©2020 ioDraw All rights reserved
Novice cloud ambassador's way of preaching --- Promotion experience of the third prince Ao Bing Learning experience of natural language processing ES6 Three simple methods of array de duplication use python Judge whether a number is 2 Of n Power python Control lightning simulator Vue+element+cookie Realize the function of remembering password Learning path of junior Embedded Software Engineer ( Bachelor or master degree ) Six Star Education :PHP Programmer's book , Let you quickly become a technical master First order low pass filter - Continuous to discrete Laravel Homestead