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 randomnumber = random.randint(1,100)print(number)count = 0while 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 = 0while number < 10: number = number + 1 if number%2==0: continue print(number)
4、for循环、print、字符串输出import datetimefor i in range(4): print("第",i+1,"次循环",sep="") #print 多个连接必须用sep作为间隔符print("-----------------------")#字符串格式化username = ‘linqian‘date = datetime.datetime.today()print(type(date))# msg = ‘欢迎‘+username+‘登录,‘+‘今天是‘+datemsg2 = ‘欢迎%s登录,今天是%s‘ %(username,date)# print(msg)print("-----------------------")print(msg2)
原文地址:https://www.cnblogs.com/lqcjlu/p/11371721.html
时间: 2024-10-11 09:17:34