码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-22 00:42:53      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:range   判断   nbsp   一个   you   lease   enter   div   强制   

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)、判断、字符串格式化

标签:range   判断   nbsp   一个   you   lease   enter   div   强制   

原文地址:http://www.cnblogs.com/ctrl/p/8082908.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!