Preface
A month has passed since the semester started, and I've been busy with coursework, leaving little time to think deeply about much else. Reading, though, has been a regular habit—just not Python books. I've mostly been reading literature. The day before the holiday, after finishing my exams, I flipped through my Python book and picked up some things that were new to me. That inspired me to improve and upgrade the project I'd been working on before the semester began, and I ended up writing a coordinate movement program.
Output Preview
The parts marked with # are my explanations; they don't appear in the actual run
Full Code
Code Analysis
Analysis Notes
The code analysis will go through what each line does, with the line numbers including the lines taken up by comments.
The random Module
Line 9's import random brings the random module into the program. Its job is to pick a random value from the dictionary, which you can see in line 12. I wrote the dictionary contents in line 11, then used random to pick one at random. If you were to print it out at this point:
It would pull any one value from spead_0 and print that, rather than always printing the first one.
Output
Before I improved the code, the program leaned on random quite a bit (see Python Learning Diary – The Story of an Outlaw). After the improvement, I switched the random value to manual input, which is even more random (not really).
The speed is still randomized, though, because that makes it easier to calculate rather than having to type it in.
Manual Input
Using input to enter values:
Here, your_坐标类型_position corresponds to the next set of conversion logic. One thing worth noting: I didn't use code like this:
a = input("Tell me your X positison:")
Output
That's because running it that way makes the output look messy. A good program balances logic and aesthetics.
Solving the Arithmetic Problem
When you use input, the value you get back can't be used in arithmetic directly—it throws an error.
Example of Broken Code
a = input("Number is ")
b = a + 3
print(f"Now your number is {b}")
Error Message
So what to do? I just went straight to copy-paste (not really). I looked at a little thing my friend had written and found you can use float() to process the input value and make it arithmetic-friendly. So it became this:
a = input("Number is ")
b= float(a)
c = b + 3
print(f"Now your number is {c}")
Output
I wrote two of these, one for the X coordinate input and one for the Y coordinate input:
# 转化X坐标
x_position = float(your_x_position)
# 转化Y坐标(+1为初始位置)
y_position = float(your_y_position)
At this point, the input value goes from being the floating value of your_坐标类型_position to the fixed value of 坐标类型_position, ready for the calculations that follow.
Printing the Original Coordinates
In the original code, it was written as
Here, {坐标类型_position} is the value we entered. Since no calculation happens at this step, you could swap {坐标类型_position} for your_坐标类型_position and it would still display fine. But to save a few bytes (and because I had nothing better to do), I'd recommend using the {坐标类型_position} style to reference the value.
Inside print(), you don't have to use \n to break lines—you can just hit Enter to move to a new line. Of course, the \n approach works too.
Oh, right—in print(), if you're only outputting text, you can just use print() like this:
print("这是一段文字")
Output
If you want to reference a value, you need to add an f in front, which stands for format (setting the format). It looks like this:
message = 文字
print(f"这是一段{message}")
Output
The if-elif-else Statement
Let's look at the original code first
This involves arithmetic, so I have to use the float() conversion I mentioned earlier before doing the math.
The numbers here can be written however you like. I made sure the smallest result wouldn't be negative, but after actually testing it, I found it doesn't matter whether it's negative or not—no bugs show up...
The most technical part here is the if-elif-else statement. Actually, it's not really technical—it's more of a logic thing.
I can translate the whole block into plain language, and then there's no need for more explanation.
And that's it. Here, I've treated else as the speed veryfast, but logically, any value that isn't slow, medium, or fast will run this logic. So if the current speed were, say, veryslow, the math would still be X+4, Y+6. But since the dictionary only has four cases, that problem never comes up.
Printing the New Coordinates
The coordinate values referenced here are the ones calculated by the if-elif-else block, so there's not much more to say.
The Introduction Section
Let's look at the code first
This packs in a lot.
First, the alien = {……} part is a dictionary, where the first column is the speed and the second column is the description. The first column values are called key, and the second column values are called value.
Each key maps to one value, aligned horizontally.
The print() after that is the simplest kind of print statement, so no need to elaborate.
Starting from if, there's another piece of logic
If the total number of entries in the alien dictionary is 4, it creates a new value, The_number = 'four', and prints the sentence There are four cases in total.
If not, it just prints the single word NULL.
This might be a bit hard to grasp, but that's the logic.
Finally, it prints out all the values in the alien dictionary
# for语句输出
for spead_1, position in alien.items():
print(f"Spead: {spead_1}")
print(f"{position}")
spead_1 corresponds to the key, and position corresponds to the value. It reads out the key and value and assigns them to spead_1 and position respectively.
Then it prints them out with print.
for is a loop: if not all the values have been printed yet, it keeps printing until every one has been output once.
Conclusion
So, is that it? This might be the most technical article I've written in nearly a year...