Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop a simple game of guessing numbers with Python

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Python to develop a simple number guessing game, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

How to use Python to make a simple number guessing game.

Rules of the game

The player will guess a number. If the guess is correct, the player wins. If not, the program prompts the player whether the guessed number is "high" or "low" compared to the actual number, and so on until the player guesses the number correctly.

Get ready for Python3.

First, you need to install Python on your computer. Can be downloaded and installed from the Python website. This tutorial requires the latest version of Python 3 (version 3.x.x).

Make sure that the box that adds Python to the PATH variable is checked. If you do not do so, it will be difficult to run the program.

Now, open the text / code editor on the device. Personally, I prefer to use Brackets. Notepad is pre-installed on Windows, Mac OS contains TextEdit, and Linux users can use Vim.

After you open the text editor, save the new file. I call it main.py, but you can name it whatever you want, as long as it ends with .py.

Coding

The instructions for this tutorial will be included in the code as comments. In Python, comments begin with # and continue until the end of the line.

From keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D # First, we need to import the 'random' module. # This module contains the functionality we need to be able to randomly select the winning number. Import random # Now, we need to select a random number. # This line will set the variable 'correct' to be equal to a random integer between 1 and 10. Correct = random.randint (1,10) # Let's get the user's first guess using the' input' function. Guess = input ("Enter your guess:") # Right now, the user's input is formatted as a string. # We can format it as an integer using the 'int' function. Guess = int (guess) # Let's start a loop that will continue until the user has guessed correctly. # We can use the'! = 'operator to mean' not equal'. While guess! = correct: # Everything in this loop will repeat until the user has guessed correctly. # Let's start by giving the user feedback on their guess. We can do this using the 'if' statement. # This statement will check if a comparison is true. # If it is, the code inside the 'if' statement will run. If guess > correct: # This code will run if the user guessed too high. # We can show a message to the user using the 'print' function. Print ("You've guessed too high. Try guessing lower.") Else: # The 'else' statement adds on to an' if' statement. # It will run if the condition of the 'if' statement is false. # In this case, it will run if the user guessed too low, so we can give them feedback. Print ("You've guessed too low. Try guessing higher.") # Now we need to let the user guess again. # Notice how I am combining the two lines of guessing code to make just one line. Guess = int (input ("Enter your guess:")) # If a user's guess is still incorrect, the code in the 'while' loop will be repeated. # If they've reached this point in the code, it means they guessed correctly, so let's say that. Print ("Congratulations! You've guessed correctly.")

In addition, you can change anything in the program at will.

For example, you can set the correct number to 1 to 100 instead of 1 to 10, and you can change what the program says in the print () function. You can write your code as much as you want.

Run the program

Depending on your operating system, open a command prompt (Windows / Linux) or terminal (Mac). Try each of the following commands in order. If Python is installed correctly, at least one of them should work.

Python C:/Users/username/Desktop/main.py py C:/Users/username/Desktop/main.py python3 C:/Users/username/Desktop/main.py

Be sure to replace C:/Users/username/Desktop/main.py with the full path to the Python file.

After the program runs, you can test it and play it several times! When you are done, press the up arrow key to copy the last command, and then press Enter to run it again.

The following is a version of the code without any comments:

Import random correct = random.randint (1,10) guess = input ("Enter your guess:") guess = int (guess) while guess! = correct: if guess > correct: print ("You've guessed too high. Try guessing lower." Else: print ("You've guessed too low. Try guessing higher.") Guess = int (input ("Enter your guess:") print ("Congratulations! You've guessed correctly.") The above is how to develop a simple number guessing game with Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report