I summarized the "input function" that is always used when doing skill check of Paiza. There are various ways to use it, so please see it when you want to check how to use it properly.
Before getting into the text, I'll write something to do before solving the skill check problem. I didn't know what to do before solving the problem at first, and I was struggling because I couldn't type in the code for about 30 minutes. I hope it helps those who solve the skill check for the first time.
① First, click Paiza_Skill Check to jump to Paiza's skill check. (2) Select and click the one you like from the list of questions for each rank on the left. (D is the gentlest and S is the most difficult) ③ After that, select your favorite question from the list and press "Challenge". ④ Then, a confirmation screen will appear, so press "Challenge the problem". ⑤ Do so in order from the top -"Problem statement" -"Evaluation points": About points -"Value to be input": Explanation of the value brought by input -"Expected output": Explanation of what you want to output -"Conditions": Lower and upper limits of numerical values -Specific input example described in "Input Value" and Specific Output Example described in "Expected Output" -"Answer column": A place to write the actual code Since it is a style, read everything and understand it. ⑥ Select the language you use from "Language" at the top right of the answer column. Then you will be able to write code. This is the preparation, so please solve your own problems from here.
I think some people think of "input" in the first place.
"Input" is to receive the data entered on the keyboard. You can receive what is in the part written as input example in Paiza's skill check by using "input". Since "print" is the output, it may be easier to understand if you think the opposite.
I will introduce the basic usage of "input".
>>>Variable name= input()
#Prompted for input
>>>print(Variable name)
#What you type is output
You can receive what you have entered in this way. It's hard to know where to type at first, but most of the time you can type it in the next line and it will be received. You can write a sentence that indicates what you want to input in "input" as shown below.
>>>Variable name= input('Instructions on what you want to enter:')
In paiza, by inputting as above, you can pull what is written in the input example. Also, what you enter will be received as a character string. In other words, even if you enter "1", it will be "'1'".
>>>number = input()
1
>>>number
'1'
>>>int(number)
1
By doing this, you can convert from a string type to a number.
In basic usage, you can only receive one value. So how do you get multiple values?
>>>a,b=input().split()
one two
>>>a
one
>>>b
two
In this way, "a" will contain "one" and "b" will contain "two". Of course, you can increase the number of "a, b, c, d ..." you can receive, but be careful as it will be difficult to see if it is too long.
And when you receive it this time, it is a character string, so if you want to receive it as a number, you should use "int".
>>>a,b=input().split()
1 2
>>>a
'1'
>>>b
'2'
>>>int(a)
1
>>>int(b)
2
But it's a little annoying if you need 3 lines to make numbers. In such a case, use "list comprehension".
It may be difficult to hear only the words, but I will explain them in order.
>>>a,b=[int(x) for x in input().split()]
1 2
>>>a
1
>>>b
2
(1) First, use the for statement to extract the elements contained in the list in "input (). Split ()]" one by one. (2) The extracted element enters "x", and the "x" is converted to a number with "int". (It's easier to understand if you look at it from behind) ③ Finally, put the converted numbers in "a" and "b". It's fascinating to be able to write in just one line what you used to write in three lines.
There is another way, so I will introduce it.
>>>N = int(input())
#Specify the number of inputs
>>>a,b = [int(input()) for i in range(N)]
1
2
>>>a
1
>>>b
2
First, ask them to specify the number of inputs, and then rotate the for statement the number of times. After that, "int (input ())" receives the elements line by line, converts them into numbers one by one, and puts them in "a" and "b". The difference from the previous one is "Is there multiple values in one line?" And "Is there one value in each line?"
>>>a,b=map(int,input().split())
1 2
>>>a
1
>>>b
2
The "map function" draws the change target in the second argument (input (). Split ()) and processes it with the first argument (int). This one is shorter and may be easier to use.
This time, I summarized "input". Paiza's skill check is essential knowledge, so it's worth remembering. Let's utilize it steadily.
Recommended Posts