python基礎實戰(一)-input,math,type

input:

demo:
輸入一個數,這個數代表從凌晨0點開始計算的分鐘數,並計算並輸出當前時間
例如:如果輸入的是150,那麼應該就是凌晨2:30,所以程序輸出也是2:30

passed_min=int(input("已經過了多少分鐘"))

hours=passed_min//60
passed_min= passed_min % 60

print("那麼現在是 ",hours, ":",passed_min)

輸出結果:

//input 數值是自己隨便輸入的。
已經過了多少分鐘200
那麼現在是  3 : 20

應用知識:input語法輸入,通過轉成int類型,才能進行加減乘除操作。
注意:算法邏輯得嚴謹,如果輸入的分鐘數超過了一天的分鐘數,該如何展示?

passed_min=int(input("已經過了多少分鐘"))
# 加上天數,用最簡單的運算法則實現功能
days=passed_min // 60 //24
hours=(passed_min//60)%24
passed_min= passed_min % 60

print("那麼現在是過了 ",days,"天之後的",hours, "小時",passed_min,"分鐘")

//輸出結果
已經過了多少分鐘1442
那麼現在是過了  1 天之後的 0 小時 2 分鐘
age=input("how old are you?\n")
height=input("How tall are you?\n")
print("You're %s years old,%s meters tall."  % (age,height))

//結果:(%s來接收輸出結果)
how old are you?
27
How tall are you?
160
You're 27 years old,160 meters tall.

math

#  the number type is integer 
print("+ and -:",1+1,1-1,1+2-3)
print("* and /:",2*3,6/3,3*4/2)
# excecute inside a () first
print("( and ):",(2+4)/(1+2))
print("%:",9%3,5%2)

//輸出結果
+ and -: 2 0 0
* and /: 6 2.0 6.0
( and ): 2.0
%: 0 1

type

# integer
print(type(2 ** 32),type(2^32))
# folat類型
print(type(4/2),type(4/3),type(4/8))
print(type(1+1),(1+1))
# str 
print(type("1"+"1"),("1"+"1"))
print(type("1+1"),("1+1"))

//輸出結果
<class 'int'> <class 'int'>
<class 'float'> <class 'float'> <class 'float'>
<class 'int'> 2
<class 'str'> 11
<class 'str'> 1+1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章