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

python 输入输出 条件判断 循环

时间:2019-08-18 11:31:48      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:else   随机数   lse   一个   linq   遇到   成绩   dom   格式化   

1、条件判断
score = int(input("请输入学生成绩:"))
if score>100 and score <0:
print("请输入正确的成绩")
elif score<=100 and score >=90:
print("优秀")
elif score < 90 and score >= 80:
print("良好")
elif score <80 and score >= 60:
print("及格")
else:
print("不及格")


2、while循环

count =0
while count<4:
print(count)
count = count+1

# break 循环中遇到break立即结束
# continue 循环里遇到continue就结束本次循环
print(‘----------------------------‘)

import random
number = random.randint(1,100)
print(number)
count = 0
while count <7:
guess = int(input("请输入一个1至100的随机数:"))
count = count + 1
if guess > number :
print("猜大了")
elif guess < number:
print("猜小了")
elif guess == number:
print("猜对了")
break
# if count != 7:
# continue
# else:
# print("错误次数用光了")
#可直接在while平级加else,while循环正常结束后会直接执行else中代码
else:
print("错误次数用光了")


3、continue
number = 0
while number < 10:
number = number + 1
if number%2==0:
continue
print(number)

4、for循环、print、字符串输出
import datetime
for i in range(4):
print("第",i+1,"次循环",sep="") #print 多个连接必须用sep作为间隔符
print("-----------------------")
#字符串格式化
username = ‘linqian‘
date = datetime.datetime.today()
print(type(date))
# msg = ‘欢迎‘+username+‘登录,‘+‘今天是‘+date
msg2 = ‘欢迎%s登录,今天是%s‘ %(username,date)
# print(msg)
print("-----------------------")
print(msg2)

python 输入输出 条件判断 循环

标签:else   随机数   lse   一个   linq   遇到   成绩   dom   格式化   

原文地址:https://www.cnblogs.com/lqcjlu/p/11371721.html

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