[Python] How to use input ()

What is input ()

__ Input __.

How to use input ()

As an example, write the code with 1 in the input.

python


a = input()

print(a)
#1
#1

As mentioned above, it can be seen that __ 1 </ font> __, which is the number of __ input __, is __input () __.

type of input ()

Take a look at the __ character _____ type </ font> __ that goes into __input () __. Is it an int, a float, or a string?

python


a = input()

print(type(a))
#1
#<class 'str'>

From the above example, it can be seen that the type __ of the entered value is __string (character) __.

Change input () to int type

Write the code below to change __input () __ to __int type __.

python


a = int(input())

print(type(a))
#1
<class 'int'>

As mentioned above, it can be seen that the 1 entered in input () is the 1 of the int (number).

Error when not int

Even if you enter a number in input () and add it as it is, an error will occur.

The following is an example.

python


a = input()

b = a + 1

print(b)
#1
Traceback (most recent call last):
  File "practice.py", line 3, in <module>
    b = a + 1
TypeError: can only concatenate str (not "int") to str

This is because the 1 you typed is recognized as the letter 1__, not the number 1. Therefore, it is necessary to change the entered 1 to the number 1.

Recommended Posts