iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

笨方法學python 大家來一起開心學python XD系列 第 4

Day4:Input 輸入

在Python 2中raw_input( )將所有輸入作為字符串(String)看待,返回字符串類型input( )只接收“數字”的輸入,在對待純數字輸入時具有自己的特性,它返回所輸入的數字的類型( int, float )

在Python 3中已取消raw_input之函數並把raw_input和input()整合,將所有輸入默認為字符串(String)處理,並返回字符串類型。

那讓我們來個程式碼練習今天的input()函數。

[In]
print ("How old are you?"),
age=input()
print('How much is this?'),
price=input()
result=f"So,you are {age} years olds and this thing is {price} dollars"
#day3學到的字串格式化
#記住前面要f然後兩邊要加"{}"
result
[Out]
How old are you?
55
How much is this?
55
'So,you are 55 years olds and this thing is 55 dollars'

然後可以在input()函數的括號裡面添加提示字,像是這樣。

e.g

[In]
age=input("What is your name?")
price=input("How much is this?")
result=f"So,you are {age} years olds and this thing is {price} dollars"
#day3學到的字串格式化
#記住前面要f然後兩邊要加"{}"
result
[Out]
What is your name? 6
How much is this? 9
'So,you are 6 years olds and this thing is 9 dollars'

今天我的筆記就到這邊XD

source:https://ppt.cc/fHC3kx


上一篇
Day3:String Formatting 字串格式化
下一篇
Day5:def函數
系列文
笨方法學python 大家來一起開心學python XD12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言