python输入一组身高_一起学Python系列之用户输入
函數input():
函數input() 讓程序暫停運行, 等待用戶輸入一些文本。 獲取用戶輸入后, Python將其存儲在一個變量中, 以方便你使用。
message=input("tell me something,and i will repeat it back to you")
print(message)
運行這行代碼會出現輸入文本框,用戶需要根據提示輸入一些文本,然后打印出來。
int()來獲取數值輸入:
height=input("How tall r u,in inches?")
height=int(height)
if height >=36:
print("\nu r tall engouh to ride!")
else:
print("\nu'll be able to ride when u r a little older.")
運行該代碼會出現提示用戶輸入自己的身高,這里為什么要int()呢,如果沒,則顯示:
TypeError: unorderable types: str() >= int()
用戶輸入的“數字”被當成字符串了,也對,input是輸入文本的。
求模運算符:
%返回余數
%將兩個數相除并返回余數
4%3
1
利用這點常可判斷奇偶:
number=input("Enter a numebr,and i will tell u if is a even or odd.")
number=int(number)
if number % 2==0:
print("\nThe number",str(number),"is even.")
else:
print("\nThe number is odd.")
我輸入的是2,這里同樣注意是str(number)
Enter a numebr,and i will tell u if is a even or odd.2
The number 2 is even.
總結
以上是生活随笔為你收集整理的python输入一组身高_一起学Python系列之用户输入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荒岛余生最后一个包裹_荒岛余生——每个人
- 下一篇: python 返回字符串长度_Pytho