1. 程式人生 > >python-循環(for、while)、判斷、字符串格式化

python-循環(for、while)、判斷、字符串格式化

pre 重復 time body 字符串格式化 一次 post int randint

python for循環

import random
random_num=random.randint(1,1000)
print(random_num);
for i in range(3):
    num=int(input(請輸入))
    if num>random_num:
        print(太大了)
    elif num<random_num:
        print(太小了)
    else:
        print(猜對了)

while循環

#循環、叠代、遍歷
#for循環
#while
#循環就是重復替你去幹嘛
#指定一個循環結束條件
#用while循環的,那麽必須得有計數器 #continue結束本次循環,繼續進行下一次循環 #break結束循環
count=0#計數器
while count<3:
    username=input(please enter your username:)
    pwd=input(please enter your pwd:)
    if username==nhy and pwd==123456:
        print(歡迎光臨)
        break
    else:
        print(賬號/密碼錯誤!)
    count
+=1 else: print(錯誤次數過多)

判斷

score=input(請輸入你的分數:)
# input接收到的全都是str類型
# int強制類型轉換
score=int(score)
if score<60:
    print(不及格)
    if score>50:
        print(hahaha)
    elif score<50:
        print(小傻瓜)
    else:
        print(-----)
elif score>=60 and score<80:
    print
(及格) elif score>=80 and score<90: print(良好) else: print(優秀)

字符串

for i in range(5):
    username=input(請輸入你的名字:)
    time=2017年12月17日 17點30分
    print(username+,歡迎光臨,+時間是:+time)#通過加號拼接兩個字符串
    print(%s,歡迎光臨,時間是:%s%(username,time))
    print(
        {name},歡迎光臨,時間是:{date},明天的時間是{date}.format(name=username,date=time)
    )

python-循環(for、while)、判斷、字符串格式化