ユーザー入力とwhileループ
- 関数入力()の動作
関数入力()はプログラムを一時停止し、ユーザーがテキストを入力するのを待ちます。入力を取得した後、Pythonはそれを変数に格納して使用します。
例:
message = input("tell me somthing, and i will repeat it back to you: ")
print(message)
tell me somthing, and i will repeat it back to you: aa
aa
input()関数を使用する場合は常に、次に何をするかをユーザーに明確に指示する必要があります。
name = input("PLease enteryour name: ")
print("\nHello, " + name + "!")
PLease enteryour name: aa
Hello, aa!
prompt = "if you tell us who you are, wo can personalize the message you see."
prompt += "\nwhat is your first name?"
name = input(prompt)
print("\nHello, " + name + "!")
if you tell us who you are, wo can personalize the message you see.
what is your first name?qq
Hello, qq!
int()を使用して数値入力を取得する:
関数input()を使用すると、Pythonはユーザー入力を次のような文字列として解釈します。
>>> age = input("how old are you:")
how old are you:21
>>> age
'21'
入力は数値21ですが、age変数の値が出力されると、実際の戻り値は「21」になります。入力数値は文字列で表されます。数値が文字列の形式で表示されている、つまり数値21が単一引用符で囲まれていると判断したのはなぜですか。 。
したがって、Pythonでユーザーが属する番号を直接使用する場合、特定の判断やチェックを行うことはできません。次のように、使用する前に文字列番号を数値に変換する必要があります。
age = input("how old are you? ")
age = int(age)
if age>= 30:
print(age)
else:
print("age too small!")
how old are you? 55
55
how old are you? 21
age too small!
height = input("how tall are you, in inches?")
height = int(height)
if height >= 36:
print("\nyou're tall enough to ride!")
else:
print("\nyou'll be able to ride when you're a little older.--command")
how tall are you, in inches?34
you'll be able to ride when you're a little older.--command
how tall are you, in inches?66
you're tall enough to ride!
モジュラス演算
数値情報を処理する場合、モジュロ演算(%)は2つの数値を除算して余りを返す非常に便利なツールです。
>>> 4%3
1
>>> 5%3
2
>>> 6%3
0
>>> 7%2
1
>>> 7%3
1
number = input("enter a number, and I'll tell you if it's even or odd: ")
number = int(number)
if number % 2 == 0:
print("\n the number " + str(number) + " is even.")
else:
print("\n the number " + str(number) + " is odd.")
enter a number, and I'll tell you if it's even or odd: 5
the number 5 is odd.
enter a number, and I'll tell you if it's even or odd: 6
the number 6 is even.
注:Python 2.7バージョンを使用している場合は、関数raw_input()を使用してユーザーに入力を求める必要があります。この関数は、python3のinput()と同様に、入力を文字列として解釈します。
Python 2.7には関数input()も含まれていますが、ユーザー入力をpythonコードとして解釈して実行しようとします。実行すると、最良の実行結果はエラーになりますが、正常に実行される可能性がありますが、独自の結果は得られません期待される結果。したがって、python2.7では、input()ではなくraw_input()を使用して入力を取得してください。
car = input("please tall me, your select car: ")
print("let me see if I can find you a " + car)
please tall me, your select car: bwa
let me see if I can find you a bwa
peopel = input("please tel me, your total many peopel eatting: ")
peopel = int(peopel)
if peopel > 8:
print("sorry, current table is full!")
else:
print("ok, have empty table.please...")
please tel me, your total many peopel eatting: 9
sorry, current table is full!
please tel me, your total many peopel eatting: 5
ok, have empty table.please...
num = input("please input int number: ")
num = int(num)
if num % 10 != 0:
print("the num: " + str(num) + " , not 10 Whole multiple")
else:
print("yes: " + str(num) + " is 10 Whole multiple")
please input int number: 5
the num: 5 , not 10 Whole multiple
please input int number: 10
yes: 10 is 10 Whole multiple
- whileループの
forループは、コレクション内の各要素のコードブロックに使用されますが、whileループは、指定された条件が満たされないまで実行を続けます。
私たちが使用する多くのプログラムには、whileループが含まれている場合があります。たとえば、ゲームは、whileループを使用して、プレーヤーがプレイしたいときに実行し続け、プレーヤーが終了したいときに停止するようにします。ユーザーがプログラムを停止しなかったときにプログラムが停止した場合、またはユーザーが終了したいときにプログラムが実行を続けた場合、それはあまりに退屈です。
次のように、whileループを使用して、ユーザーが望むときにプログラムを実行し続けます。
prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""
while message != 'quit':
message = input(prompt)
print(message)
tell me somthing, and i will repeat it back to you:
enter 'quit' to end the program.go
go
tell me somthing, and i will repeat it back to you:
enter 'quit' to end the program.quit
quit
------------------
(program exited with code: 0)
Press return to continue
もちろん、フラグを使用して実行を継続するかどうかを決定することも
できます。実行を継続する前に多くの条件を満たす必要があるプログラムでは、変数を定義してプログラム全体がアクティブかどうかを判断できます。この変数は標識は次のようなプログラムの信号機として機能します。
prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
tell me somthing, and i will repeat it back to you:
enter 'quit' to end the program.go
go
tell me somthing, and i will repeat it back to you:
enter 'quit' to end the program.do
do
tell me somthing, and i will repeat it back to you:
enter 'quit' to end the program.quit
------------------
(program exited with code: 0)
Press return to continue
ブレークを使用してループを終了します。次に例を示します。
prompt = "\nplease enter the name of a city you have visited: "
prompt += "\n(enter 'quit' when you are finished)"
while True:
city = input(prompt)
if city == 'quit':
break
else:
print("i'd love to go to " + city.title() + "!")
please enter the name of a city you have visited:
(enter 'quit' when you are finished)go
i'd love to go to Go!
please enter the name of a city you have visited:
(enter 'quit' when you are finished)go
i'd love to go to Go!
please enter the name of a city you have visited:
(enter 'quit' when you are finished)quit
------------------
(program exited with code: 0)
Press return to continue
ループでcontinueを使用します。continueステートメントは、breakステートメントのようにプログラムを直接終了しませんが、現在のループを無視しますが、次のループの先頭に直接ジャンプします。
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
1
3
5
7
9
------------------
(program exited with code: 0)
Press return to continue
余りが0でない場合は出力し、余りが0の場合は次のサイクルを実行します。
注意:ループプログラムを作成するときは、プログラムが無期限に実行されるように、デッドループを回避する必要があります。このようなシーンが必要でない限り、無限ループが使用されます。それ以外の場合は、ループ内のループを終了する必要があります。状態。