How to use Python to realize the number guessing game
This article mainly shows you "how to use Python to achieve number guessing game", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python to achieve number guessing game" this article.
Basic environment configuration
Version: Python3
System: Windows
Game of guessing numbers
Mainly involves knowledge points: basic data types, loops (for loop, while, if..else)
Go directly to the code, the comments on the code are very clear
Import random# calls random module num = random.randint (1500) # randomly generates a number from 1 to 500 and assigns a value to numprint ('guess how big it is') time = input () # enter the value you guessed temp = int (time) # convert your guessed value to the number of times you guessed # generate a True loop and then use if else The elif statement further excludes the range of wrong numbers and adds 1while True: if temp = = num: print ('guessed right') if I = = 1: print ('got it right the first time') break# checks the value of I when the input result is equal to the randomly generated result and checks the value of I when the input result is equal to the randomly generated result. Print out the corresponding string elif 500 > = temp > num: print ('too big, Sao Nian') print ('come on, the scope is further narrowed') if 10