In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to write a simple game with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In this series, we will write the same application in different programming languages to compare how various languages work, and show that using standard test procedures is a good way to learn new programming.
When you learn a new programming language, it's good to focus on what they have in common. Variables, expressions, and statements are the basis of most programming languages. Once you understand these concepts, you can start to figure out the rest.
Because programming languages have many similarities, once you know one language, you can usually learn the basics of another language by observing how it differs from what you know. Using standard test programs you write in other languages allows you to focus on the language rather than the logic of the program.
To prove this, we are testing how to write a "guessing number" program in multiple languages. The computer chooses a number between 1 and 100 and asks you to guess. The program loops until you guess the correct answer.
The number guessing program practices several concepts of the programming language:
Variable
Input
Output
Conditional judgment
Cycle
This is a good practical experiment for learning a new programming language.
Guessing numbers with Python
In the words of the Python Software Foundation, "Python is an explanatory, interactive, object-oriented programming language that contains modules, exceptions, dynamic types, very high-level dynamic data types and classes." It is a good general-purpose programming language, ranging from simple scripts to complex GUI applications.
You can explore Python by writing a version of the number guessing game. This is my realization:
Import random as randomlibrandom = randomlib.randint (1,100) print ("Guess a number between 1 and 100") while True: guess = int (input ()) if guess
< random: print("Too low") elif guess >Random: print ("Too high") else: print ("That's right!") Break
To assign a value to a variable, list the name of the variable, followed by the = sign. For example, the statement random = 0 assigns a zero value to the random variable.
The random module is imported on the first line of the script. Since all programs in this series use random as the name of the variable, you can use import random as randomlib to import it under an alias to avoid naming conflicts.
Few functions are built into Python, and most functions must be explicitly imported from the standard library. The random standard library module has the function of generating various random values.
The second line of the script reads the result of the function randint () and assigns it to a variable named random. The function requires two parameters: a minimum and a maximum. In this case, the range is 1 to 100 to make the game challenging enough.
You can use the input () function to prompt the user for a value. If you write guess = int (input ()), Python waits for the user to enter some text, converts it to an integer, and stores the value in the guess variable.
Python supports flow controls such as conditional expressions and loops. In the "guessing numbers" game, the loop continues as long as the value in guess is not equal to random,Python.
If the guess is less than a random number, Python prints Too low, and if the guess is greater than this number, Python prints Too high.
Sample output
Now that you have written the Python program, run it to play the "guessing numbers" game. Each time you run the program, Python randomly selects a different number. To complete the game, you need to guess until you find the right number:
$python guess.pyGuess a number between 1 and 100 50Too high 25Too high 12Too high 7Too high 3Too low 5Too low 6That's right!
This "guessing numbers" game is a good starter when learning a new programming language because it practices several common programming concepts in a fairly straightforward way. By implementing this simple game in different programming languages, you can show some of the core concepts of different programming languages and compare the details of each language.
This is the end of "how to write a simple game in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.