兩分鐘了解python中的input函數

烟雨青岚
發布: 2020-06-28 12:44:46
轉載
7191 人瀏覽過

兩分鐘了解python中的input函數

兩分鐘了解python中的input函數

input函數在python中是一個內建函數,其從標準輸入中讀入一個字串,並自動忽略換行符。下面我們就來看看input函數的具體用法吧。

#函數input

message = input("Tell me something, and I will repeat it back to you: ")
print(message)
message1 = input()
print(message1)
登入後複製

兩分鐘了解python中的input函數
##建立多行字串的方式

prompt1="type someting"
prompt2=prompt1+": "+input()
print(prompt2)
登入後複製



prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
登入後複製


兩分鐘了解python中的input函數
#input預設輸入是字符,所以需要字符比較時應進行類型轉換

age=input("How old are you?")
age=int(age)
if(age>=18):
print("You have the right to vote!")
else:
print("Sorry,you have no right to vote!")
登入後複製


##求模運算子

c1=input("Please input a number,and i will do modulo operation for it.")
c1=int(c1)
c2=input("Please input another number,and i will do modulo operation for it.")
c2=int(c2)
c3=c1%c2
print(c3)
登入後複製

兩分鐘了解python中的input函數
#汽車租賃:寫一個程序,詢問使用者要租賃什麼樣的汽車,並列印一條訊息,如「Let me see if I can find you a Subaru」。

input("What kind of car would you like to hire?")
print("Let me see if I can find you a Subaru")
登入後複製

兩分鐘了解python中的input函數
#餐廳訂位 :寫一個程序,詢問用戶有多少人用餐。如果超過8人,就列印一則訊息,指出沒有空桌;否則指出有空桌。

p1=input("How many people in the dinner?")
p1=int(p1)
if(p1>8):
print("Sorry,There is no empty table.")
else:
print("ok,There is an empty table")
登入後複製


#10的整數倍 :讓使用者輸入數字,並指出這個數字是否是10的整數倍。

n1=input("Please input a number")n1=int(n1)if(n1%10==0):print(n1+" is an integer multiple of 10.")else:
print(n1+" is not an integer multiple of 10.")
登入後複製


input函數在python中是一個內建函數,其從標準輸入中讀入一個字串,並自動忽略換行符。

也就是說所有形式的輸入按字串處理,如果想要得到其他類型的資料進行強制型別轉換。預設沒有

提示字串(prompt  string),在給定提示字串下,會在讀入標準輸入前標準輸出提示字串。如果遇到

檔案結束符號(end of file)會觸發一個EOFError。

感謝大家的閱讀,希望大家收益多多。

本文轉自:https://blog.csdn.net/qq_24135817/article/details/79818479

推薦教學:《python教學

以上是兩分鐘了解python中的input函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!