python 输入输出 条件判断 循环
Posted lqcjlu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 输入输出 条件判断 循环相关的知识,希望对你有一定的参考价值。
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 输入输出 条件判断 循环的主要内容,如果未能解决你的问题,请参考以下文章