python循环(for、while)、判断、字符串格式化

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)
    )
原文地址:https://www.cnblogs.com/ctrl/p/8082908.html