In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Python how to achieve the number guessing game", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve the number guessing game" bar!
Game introduction:
The program itself has a number, the user enters a number, two numbers are compared.
Knowledge points:
Input function
String
While cycle
If conditional judgment statement
Break statement
Start playing the code.
Let's take a look at the effect picture first.
Code
# 1. There is a number my_num = number 2. Enter a number you_num = input ("Please enter a number:") you_num = int (you_num) # 3. Guess the number result # one equals sign is copy sign two equals sign is equal sign print (my_num, you_num) print (type (my_num), type (you_num)) # between different types, it must not be equal if my_num = = you_num: print (congratulations on your guess, the current number is {my_num}') else: print (you are so lame, you can guess wrong The current number is {my_num}, and the number you entered is {you_num}')
Upgrade a little bit
My_num = "if I want to guess three times N times until I guess the right position times = 1while True: print (f 'begins {times} guessing the number') you_num = input (" Please enter a number (1-10): ") # if the input is not a number if you_num.isdigit (): you_num = int (you_num) if My_num = = you_num: print (congratulations on your guess The current number is {my_num}') break # enter the 22nd line # elif my_num > you_num: else: # guess wrong to enter the 23rd line # print (f 'you are so lame, you can guess wrong, the current number is {my_num}, the number you entered is {you_num}') # print ('you are too stupid You can guess wrong') if my_num > you_num: print ('the number is too small') else: print ('the number is too big') if times = = 3: print (f 'you don't have a chance') break times + = 1 else: print ('enter a number incorrectly, please enter a number')
Effect picture
Let's have a final version
# random is a built-in random function module import randommy_num = random.randint (1,100) times = 1while True: print (f 'starts the {times} number guess') you_num = input ("Please enter a number (1-100):") you_num = int (you_num) if my_num = = you_num: print (f 'Congratulations on your guess The current number is {my_num}') break else: if my_num > you_num: print ('number is too small') else: print ('number is too big') times + = 1
Effect picture
Thank you for your reading, the above is the content of "how to achieve the game of guessing numbers in Python". After the study of this article, I believe you have a deeper understanding of how to achieve the game of guessing numbers in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.