LoginSignup
1
0

More than 3 years have passed since last update.

自習” A Beginner's Guide to Getting User Input in Python”の和訳

Last updated at Posted at 2021-01-11

元記事URL https://hackernoon.com/a-beginners-guide-to-getting-user-input-in-python-141q312q

自習用の翻訳なので、自己責任でご利用ください。

A Beginner's Guide to Getting User Input in Python(初心者向けガイド Pythonでデータを入力する方法)

January 8th 2021
2021年1月8日

29079D8A-0671-4DF0-8C5B-281A33731DFA.jpeg

@aswinbarath
Aswin Barath
Budding Software Engineer
@aswinbarath
オースウィン・バラス
新人ソフトウエアエンジニア

Getting input from the user is what makes a program more interactive with the user.
ユーザから入力を受け付けることは、ユーザにとってプログラムをよりインタラクティブなものにします。

Hence, in python we have an input function: input(), to receive input from the user.
ですから、ユーザからの入力を受け取るために、Pythonには入力のための関数 input()があります。

Take a look at an example.
次のサンプルコードを見てください。

B5BDCCF8-EA2D-476F-9274-CC484680B005.png

Output:
画面出力は次のようになります。

Enter any data:Happy new year!
Happy new year!

Input Function Under the Hood(入力関数の内部の動き)

When input() function is encountered by python, the program will pause until the user enters data.
Pythonインタプリタが input() 関数を実行すると、ユーザがデータを入力するまでプログラムは一時停止します。

Later, any input entered by the user will be converted into a string. Let’s see another example to understand this point.
その後に、ユーザが何かしらのデータを入力すると、そのデータは文字列オブジェクトに変換されます。これを理解するために、次のサンプルコードを見てください。

6CAC671F-E665-4AB1-B14C-FAB6DDB87812.png

Output:
画面出力は次のようになります。

Enter any text: Have a great year ahead.
text: Have a great year ahead. , type: <class 'str'>
Enter any number: 2021
number: 2021 , type: <class 'str'>

So, in these cases make sure that you convert the input to your preferred data type using its corresponding constructors. Let’s see that example too.
このため、以上の場合には対応するコンスタラクタ(入門者が読むならば、ここでは変換手段)を使って入力されたデータを望むデータ型に変換する必要があります。次のサンプルコードも見てください。

80AAAEF2-C6AC-43C0-9C12-7A5EE17E0401.png

Output:
画面出力は次のようになります。

Enter any number: 2021
number: 2021 , type: <class 'int'>

So, code along and have fun :)
と、いうことで、コーディングを楽しみましょう!(^v^)

1
0
1

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
0