1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【python】input()の使い方

Posted at

input()とは

__入力__である。

input()の使い方

例として入力に1を入れたコードを書く。

a = input()

print(a)
# 1
# 1

以上のように__入力した数__である__1__が__input()__になっていることが分かる。

input()の型

input()に入る__文字の__を見てみる。intなのかfloatなのかstringなのか。

a = input()

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

以上の例から、__入力した値の型__は__string(文字)__になっていることが分かる。

input()をintの型に変更

__input()__を__intの型__に変更するコードを以下に書く。

a = int(input())

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

以上のようにinput()に入力した1がint(数字)の1になっていることが分かる。

intにしないときのエラー

input()に数字を入れてそのまま足し算をしてもエラーになる。

以下がその例。

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

これは__入力した1が数字の1ではなく文字の1__だと認識されている。
そのため、__入力した1を数字の1に直す__必要がある。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?