python 输入输出 条件判断 循环

时间:2021-06-03 13:15:19
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("不及格") #若if代码块中无内容,可使用pass,pass代表空代码块,无实际意义
a = 10
if a%2 == 0:
pass
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)