Python學習(八)使用者輸入 函式 inpt

2022-08-21 05:51:07 字數 1857 閱讀 6926

函式 inpt()工作原理

函式 inpt() 讓程式暫停執行,等待使用者輸入一些文字。獲取使用者輸入後,將值儲存到乙個變數中,值得型別為字串

message = input("tell me something, and i will repeat it back to you:")

print("===>>:%s"%(message))

tell me something, and i will repeat it back to you:hello everyone!

===>>:hello everyone!

程式提示語句要清晰

使用input()函式時,需要指出使用者需要輸入的資訊內容

name = input("please enter your name :")

print("===>>:hello %s !"%(name))

please enter your name :jorbabe

===>>:hello jorbabe !

input()獲取數值輸入
age = input("how old are you :")

print("===>>: %s type:%s "%(age,type(age)))

how old are you :15

===>>: 15 type:

轉換輸入的數字型別int

age = input("how old are you :")

age = int(age)

print("===>>: %s type:%s "%(age,type(age)))

how old are you :15

===>>: 15 type:

求模運算子

處理數值資訊時,求模運算子(%)是乙個很常用的工具,將兩個數值相除並返回餘數

age = input("enter a number, and i'll tell you if it's even or odd:")

age = int(age)

print("===>>: %s type:%s "%(age,type(age)))

if age % 3 == 0 :

print("\nthe number %s is even ."%(age))

else:

print("\nthe number %s is odd ."%(age))

enter a number, and i'll tell you if it's even or odd:15

===>>: 15 type:'int'>

the number 15 is even .

enter a number, and i'll tell you if it's even or odd:16

===>>: 16 type:'int'>

the number 16 is odd .

python 2.7 獲取輸入

在python 2.7中輸入模組使用 raw_input()

2.7中的input() 模組解讀輸入內容為執行**,請勿使用

python請求使用者輸入 使用者輸入

使用者輸入 大多數程式都旨在解決終端使用者的問題,為此通常需要從使用者那裡獲取一些資訊。例如,假設有人要判斷自己是否到了投票的年齡,要編寫回答這個問題的程式,就需要知道使用者的年齡,這樣才能給出 答案。因此,這種程式需要讓使用者輸入其年齡,再將其與投票年齡進行比較,以判斷使用者是否到了投票的年齡,再...

python使用者輸入

使用者輸入 python2.0 name raw input input your name raw input 輸入接收的是字串和數字,python都認為是字串。並賦值給name name input input your age input 輸入接收的是數字和字元,input python認為輸...

python 使用者輸入

範例1 我們希望整數 整數 這就是為什麼我們使用int 函式。x int raw input enter x y int raw input enter y sum x y print sum 輸出 enter x 33 enter y 556 589如果想要輸入浮點數,那麼應該使用 float r...