[new medical minor Python homework] judge quadrant problems -- skilled use of string operation and loop

Knowledge introduction

character string

Concept: string is mainly used for programming. See the text for concept description, function explanation and usage details. Here is a supplementary point: string is similar to character array in storage, so a single element of each bit can be extracted, such as s = "abcdefghij", then s[1] = "b", s[9] = "j", which can provide us with a lot of convenience, For example, in high-precision operation, each bit can be converted into a number and stored in the array.

subject

Enter a coordinate separated by commas (in English format!) and output the quadrant in which the coordinate is located

code

# This is the annotated code
# made by DavidChen 2021.12.4
# Judgment quadrant.py
import sys # Load a forced exit library. It is used when sys.exit(0) is used to force the exit of the program
s = input("Enter coordinates in the format x,y: ") # The input string is passed to s
xx, yy = 0, 0 # initialization

# The operation below is to find the position of the English comma in s
for i in range(0, len(s)): # Running cycle
    if s[i] == ',':
        break # If it is found, break directly. At this time, the i corresponds to the position of the comma
# At this time, the cycle has been out        
if i == len(s)-1: # Judgment if the input string has no reply made by comma, if it is to hand in an assignment, this judgment can not be written
    print("Incorrect input format, missing English comma")
    sys.exit(0)

# The following is to use the int() function to convert the first half and second half of the comma into a number type for further judgment
x, y = int(s[0:i]), int(s[i+1:])
# The following is a simple judgment quadrant, very simple
if x == 0 and y != 0:
    print("Coordinates at Y On shaft")
elif y == 0 and x != 0:
    print("Coordinates at X On shaft")
elif x*y == 0:
    print("The coordinates are at the origin")
else:
    if x > 0 and y > 0:
        print("The coordinates are in the first quadrant")
    if x > 0 and y < 0:
        print("The coordinates are in the fourth quadrant")
    if x < 0 and y < 0:
        print("The coordinates are in the third quadrant")
    if x < 0 and y > 0:
        print("The coordinates are in the second quadrant")


Knowledge points involved in the code

loop

Python loops mainly include While and For loops. Their formats are as follows:

# While Loop 
while <condition>: 
	<Execute statement>

For example, if I want to find the sum value of 1 + 2 + ··· + 100, how can I use the while loop and the for loop respectively?
Obviously, make a simple accumulation

sum = 0 # Initialize cumulative value
i = 1 # Initialize first value
while i<=100:
	sum += i # Cumulative process
	i += 1 # accumulator
print(sum)
# For loop
for i in range(You can fill in a single value or start and end points): 
	<Execute statement>

Tip:

Some instructions, in range If the number filled in the form indicates the number of times to run in a cycle, for example, the data is 100, then it indicates for After 100 cycles, at this time i The value of is taken from 0 to 99, that is[0,99],Exactly a hundred times;

If you fill in two data, for example range(2,10),So at this time i The actual value is[2,10),mean[2,9],Note that the latter is actually unavailable in the loop.

for In the loop, the accumulator defaults to 1, that is, if it is not necessary to execute the statement, it is not necessary to add the accumulator because for During the cycle, it will automatically accumulate by 1. If you need to modify the default step size, add a value after the start and end points to be the new step size
for i in range(1,10,2):
	print(i)

result:

1
3
5
7
9

Back to the accumulation problem, this string of code is very simple

# 1
sum = 0
for i in range(100):
    sum += i+1
print(sum)

# 2
sum = 0
for i in range(1,101):
    sum += i
print(sum)

judge

In this small problem, our judgment is

if s[i] == ',':

At this time, we compare each bit in the string with ','. In fact, the computer converts each letter in the string into ASC code and compares it with the ASC code of ','.

String operation in code

The string operation of this problem is mainly

x, y = int(s[0:i]), int(s[i+1:])

Students in this line who don't understand Kang can take it apart. That's it:

x = int(s[0:i])
y = int(s[i+1:])

They mean the same thing
s[a:b] here refers to bit a to bit b-1 of s string, that is, [a, b]. Have you found that this is the same as the circular statement above, which is a point that novices often need to pay attention to
The outer nested int() function is to convert the things in parentheses into int form, that is, the purpose we want to achieve is to express the first half and the second half of the coordinate string.

That's all for today's explanation. Because this article was written secretly in the data class, some places may be superficial or too colloquial (I haven't seriously written code and read code books for a long time ~ ~ ~ ~) where I can't understand, I can reply in the comment area. I'll correct it in time.

Keywords: Python Back-end

Added by macewan on Sun, 05 Dec 2021 03:58:15 +0200