This article was first published in the public number “ape Tiangang”, reproduced please indicate the source, thank you!

Sometimes we write programs that need to “get” data from the user in order to proceed, for example, by requiring the user to enter his or her name and age to determine whether someone is of legal voting age.

Python is much easier to “retrieve” user input data than Java, just by learning the input() method.

Let’s learn the basic usage of the input() function and take the first step in writing an interactive program.

1. How input() works

The input() method prints a prompt to the console (more on that later if you have set it), then pauses the program and waits for the user to enter some text data. Once you get the data entered by the user, Python stores it in a variable for later use.

For example, the following program asks the user to enter an age (not yet determined whether they can vote) and then prints out the user’s age:

age = input("How old are you, little friend? Please enter your age below: \n")
print("My year" + age + "Age")
Copy the code

The input() function takes an argument, which is a prompt or description to display to the user so that the user knows what to do.

In the example above, the argument we pass to the input() function is the child. How old is the child? Please enter your age below: \n. The program prints this parameter to the console, where the user can see it (hint).

Then the user input his age according to the prompt, after the input is finished, when the user presses the Enter key, the program will continue to execute, and the user input is stored in the variable age, which is finally printed out by us.

To be clear, the console can be slightly different from IDE to IDE. I used PyCharm and the user input was in color. Don’t be surprised if you are using another IDE without color.

Although the above program is short, it actually implies a lot of information:

  1. If you need to prompt the user, you pass the prompt (usually a string) to the input() method. Of course, you can also upload nothing and the application will still work, but the user experience is not very good;

  2. Whatever the user enters, Python converts it to STR.

On the second point, let’s look at it in detail:

age = input("How old are you, little friend? Please enter your age below: \n")
print(type(age))
Copy the code

Age = age; age = age;

You can see that the input “18” is indeed of STR type, which explains why age can be concatenated with the string without being converted.

2. Voting procedures

Now that we know the basic usage of the input() function, we are ready to implement the voting program. But before we do that, we need to catch up on a little bit of knowledge.

We know that the input() function takes user input that is theoretically of type STR (string).

“18” is a string and 18 is a number. Only the number 18 can be directly compared to something greater than, less than, or equal to.

Question: How do I convert STR to an integer?

A: Use functionsint()The argument is the string to be converted.

age = input("How old are you, little friend? Please enter your age below: \n")
print(type(age))
# convert STR to int
n = int(age)
print(type(n))
Copy the code

We converted STR age to int and assigned it to n, so type(n) should output int.

As you can see from the picture, the transformation really worked.

** STR is converted to int, using the int() function. The converted string must be an integer but of type STR.

Now everything is ready to implement the voting process:

name = input("Please enter your name:")
age = input(Please enter your age:)
age = int(age)
if age >= 18:
    print(name + "You are of legal voting age!")
else:
    print(name + "Unfortunately, you will not be able to vote at this time!")
Copy the code

First, two variables name and age are defined to receive the name and age entered by the user. Note that both variables are of type STR at this point.

We then need to compare the age entered by the user with the legal voting age (18), so we need the user to enter an integer age, so we use the function int(). We’ll talk about the concept of functions later, but if you don’t have the concept, just think of it as a black box, don’t bother, just know that int(), with some manipulation, can convert STR to int.

Finally, compared with 18, greater than or equal to 18 can vote, otherwise can not vote.

Running results of 18 years old or older:

Running results under 18 years old:

3. Judge odd and even numbers

I’m writing this to strengthen my practice with the input() function, and also to talk about an important operator that I didn’t cover in previous articles: the % modulo operator.

You may be familiar with addition (+), subtraction (-), multiplication (*), division (/), and power (**). For those of you who have forgotten, click on this article.

The modular operator calculates the remainder of the division of two numbers.

To compute the graphical expression with the modulo operator:

23 % 5
Copy the code

It’s got to be 3.

Anyway, how do you know if something is odd or even?

We know that even numbers are divisible by 2, and odd numbers are not divisible by 2. Exactly divisible means zero remainder, which is exactly where the modulo operator % excels.

Suppose there is some integer X, if:

X % 2= =0 The remainder of # X and 2 is 0
Copy the code

That means X is even, otherwise X must be odd.

After understanding the basic idea, start programming:

number = input("Please enter an integer and the program will automatically determine even and odd:")
number = int(number)

if number % 2= =0:
    print("\ n Numbers" + str(number) + " 是偶数")
else:
    print("\ n Numbers" + str(number) + "Is odd.")
Copy the code

First, we define the variable “number” to receive the integer input by the user. In mind, we need to know that “number” receives the type STR, which cannot directly perform the modulo operation with 2.

Then convert STR number to an int, and number and 2 can be modulo evaluated.

If the remainder of number divided by 2 is 0, it is an even number; otherwise, it is an odd number.

Running results:

Modular operators are used a lot in programming, and are just as important as addition, subtraction, multiplication, and division, so keep them in mind if you haven’t used them before.

4. Conclusion

This article focuses on the basic usage of the input() function, how to convert STR to int, and the use of the modulo operator.

If this article has helped you, please like it, share it and bookmark it

Or if you have any questions, feel free to contact me on the public account below, where articles about Python will be published.

Finally, thanks for reading, and we’ll see you next time.