Python入门学习笔记(二)

Day2

For循环

For循环

Tips:range(10)=(0,10)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(list(range(10)))
out:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
age=26
for i in range(3):
	geuss = int(input("输入你的猜测:"))
	if geuss> age:
		print("猜大了")
	if geuss< age:
		print("猜小了")
	else:
		exit("猜对了")#用exit可在打印文案的同时高亮文案并退出循环

循环嵌套

例子,打印楼层:

#L2-202室
for i in range(1,6):#range(m,n):m~(n-1)
    print(f"-----------{i}层---------")#1-5层
    for j in range(1,9):#1-8室
        print(f"L{i}-{i}0{j}室")

out:-----------1层---------
L1-101室
L1-102室
L1-103室
L1-104室
L1-105室
L1-106室
L1-107室
L1-108室
-----------2层---------
L2-201室
L2-202室
L2-203室
L2-204室
L2-205室
L2-206室
L2-207室
L2-208室
-----------3层---------
L3-301室
L3-302室
L3-303室
L3-304室
L3-305室
L3-306室
L3-307室
L3-308室
-----------4层---------
L4-401室
L4-402室
L4-403室
L4-404室
L4-405室
L4-406室
L4-407室
L4-408室
-----------5层---------
L5-501室
L5-502室
L5-503室
L5-504室
L5-505室
L5-506室
L5-507室
L5-508室

break&continue

continue:结束本次循环,直接进入下一次循环
break:结束整个循环(在多层嵌套循环中体现为结束某一层循环

例子:不走第三层,遇到L4-404直接不走剩下的所有楼层

for i in range(1,6):
    print(f"-----------{i}层---------")
    if i==3:
        print("不走")
        continue #跳出i=3此次循环
    for j in range(1,9):
        if i==4 and j==4:
            break  #跳出j循环
        print(f"L{i}-{i}0{j}室")
    if i==4:#i==4,j==4程序break跳出j循环,往后运行,来到这里,使得不走剩下的楼层。
        break

out:-----------1层--------- L1-101室 L1-102室 L1-103室 L1-104室 L1-105室
L1-106室 L1-107室 L1-108室
-----------2层--------- L2-201室 L2-202室 L2-203室 L2-204室 L2-205室 L2-206室 L2-207室 L2-208室
-----------3层--------- 不走
-----------4层--------- L4-401室 L4-402室 L4-403室

While循环

def:

while 条件: #当条件为真时,就会不断循环
	print(xxx)

例题:规定猜年龄游戏的次数

count=0
age=20
while count<3:
    count +=1
    guess=int(input("请输出你的猜测"))
    
    if guess>20:
        print("猜大了")
    elif guess<20:
        print("猜小了")
    else:
        print("恭喜猜对了")
        break

例题

1.打印99乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print(f"{i}*{j}={i*j}",end=" ")
    print()

out:
99乘法表

2.京牌车牌摇号:
需求:
(1)允许用户最多选3次
(2)每次放出20个车牌供用户选择
(3)格式:京[A-Z]-[XXXXX],可以是数字和字母的组合
代码:

import random
import string
print("京牌摇号小程序".center(50,"-"))
count=0
while count<3:
    car_nums=[]  #存储供用户选择的号码
    count+=1
    print(f"第{count}次摇号".center(50,"-"))

    for i in range(20):
        s1 = random.choice(string.ascii_uppercase)
        s2 = "".join(random.sample(string.ascii_uppercase + string.digits, 5))#sample()内左为大写字母与数字组合
        car_num=f"京{s1}-{s2}"
        car_nums.append(car_num)
        print(i+1,f"京{s1}-{s2}")
    choice=input("请输入你选择的摇号:").strip() #.strip()用途为把用户输入内容前后的空格去掉
    if choice in car_nums:
        print(f"恭喜你选择了新车牌号:{choice}")
        exit("Good luck.")
    else:
        choice2=input("您输入的号码不正确,重新输入或输入数字0重新摇号:")
        if choice2==0:
            continue
        elif choice2.strip() in car_nums:
            print(f"恭喜你选择了新车牌号:{choice2}")
            exit("Good luck.")

3.年会抽奖程序:一二三等奖分别3,6,30名。
需求:
(1)共抽三次,第一次抽三等奖,第二次抽二等奖,第三次抽一等奖。
(2)每个员工限中奖一次,不能重复。

import random
print("年会抽奖小程序".center(50,"-"))
count=0
price=3
while count<3:
    count+=1
    staff_nums=list(range(300) ) #存员工号码
    if price==3:
        print("三等奖抽奖ing".center(50,"-"))
        third=random.sample(staff_nums,30)
        print("恭喜持有以下号码的30名员工获得三等奖1000元:")
        print(third)
        for i in range(30):
            staff_nums.remove(third[i])
    elif price==2:
        print("二等奖抽奖ing".center(50, "-"))
        second=random.sample(staff_nums,6)
        print("恭喜持有以下号码的6名员工获得二等奖iPhone1台:")
        print(second)
        for i in range(6):
            staff_nums.remove(second[i])
    else:
        print("一等奖抽奖ing".center(50, "-"))
        first = random.sample(staff_nums, 3)
        print("恭喜持有以下号码的3名员工获得一等奖泰国五日游:")
        print(first)
    price-=1
print("本次抽奖结束, 恭喜以上获奖员工!")

out:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值