【无标题】

本文详细介绍了Python新手如何使用if、if...else、if...elif...else语句进行条件判断,以及while和for循环的基本操作,包括嵌套和跳出语句。通过实例演示了购物节优惠逻辑和猜数字游戏等实战应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小白学Python DAY1(补充)

1.条件判断语句
    1.1 if
        格式:if 条件:
                条件成立要执行的语句
'''if
    产生随机数
'''
import random
ran=random.randint(1,10)#从1-10中任意取出一个随机数
print(ran)#自检时写
guess=input('请输入你猜的数:')
if guess==ran:
    print('猜对了')
else:
    print('猜错了')
    1.2 if...else
        格式:if 条件:
                条件成立要执行的语句
             else:
                条件不成立要执行的语句
'''if eles
    1.产生两个随机整数1-10,判断两个数字之间的和是否大于8并且差小于3,如果是则显示:success  否则显示:fail
    2.输入账号和密码(名:admin,密码:1234),判断是否登录成功,正确显示登录成功,否则显示失败
'''
#练习1
import random

ran1 = random.randint(1, 10)
ran2 = random.randint(1, 10)
# print(ran1)#自检时添加
# print(ran2)#自检时添加
if ran1 + ran2 >= 8 and abs(ran1 - ran2) <= 3:  # and--->有假则假,abs()---->取绝对值
    print('success')
else:
    print('fail')

#练习2
username = input('用户名:')
password = input('密码:')
if username == 'admin' and password == '1234':
    print('登录成功')
else:
    print('用户名或者密码输入有误!')
# if eles练习--->3.输入一个四位数,判断相加和是否大于10
a = int(input('请输入一个四位整数:'))
if (a // 1000) + (a // 100 % 10) + (a // 10 % 10) + (a % 10) > 10:
    print('success')
else:
    print('fail')
    1.3 if...elif...elif...
        格式:if 条件1:
                条件1为真执行的语句
            elif 条件2:
                条件2为真执行的语句
            elif 条件3:
                条件3为真执行的语句
                ...
            else:
                1,2,3...条件都不成立所执行的语句
'''if...elif......else
    1.输入销售金额,符合那种奖励的范围
    2.人员管理系统框架  功能:增删改查
'''
# 练习1
sales_amount = int(input('销售金额(万):'))  # sales_amount:销售金额
if sales_amount >= 1 and sales_amount < 5:
    print('奖励1000元')
elif sales_amount >= 5 and sales_amount < 10:
    print('奖励笔记本')
elif sales_amount >= 10 and sales_amount < 100:
    print('奖励手机')
elif sales_amount >= 100:
    print('奖励一辆车')
else:
    print('鼓励奖:毛绒玩具')
# 练习2
print('-------欢迎进入小太阳人员管理系统------')
choice = input('请选择功能模块:\n1.添加员工信息\n2.删除员工信息\n3.查询员工信息\n4.删除员工信息\n')
if choice == '1':
    # 1.添加员工信息
    print('添加员工')
    pass
elif choice == '2':
    # 2.删除员工信息
    print('删除员工')
    pass
elif choice == '3':
    # 2.修改员工信息
    print('修改员工信息')
    pass
elif choice == '4':
    # 2.查询员工信息
    print('查询员工')
    pass
else:
    print('输入有误!请重新输入')
   1.4 if 语句的嵌套
        if 条件:
            if 条件:
                pass
            else:
                pass
        else:
            pass
'''
嵌套
'''
n = 6
if 8 > 6:
    print('------')
    if n < 10:
        print('6<10')
    else:
        print('错误')
    print('*****')
else:
    print('+++++++')


'''
if嵌套大练习----->购物节
用户,消费总金额,账户金额,优惠劵
输入总购买金额数:
    0<=金额<500,lv1级别
    500<=金额<2000,lv2级别
    金额>=2000,lv3级别
lv1:随机赠送3张1-10元优惠劵
lv2:随机赠送2张50元优惠劵,如果充值则送充值金额的10%
lv3:随机赠送2张100元优惠劵,如果充值则送充值金额的20%
'''
import random

username = '小丽'
total = 2000  # 消费总金额
money = 0  # 账户金额
coupon = 0  # 优惠券
if 0 <= total < 500:  # lv1
    quan1 = random.randint(1, 10)
    quan2 = random.randint(1, 10)
    quan3 = random.randint(1, 10)
    coupon = quan1 + quan2 + quan3
    print(coupon)
    money += coupon
    print('目前账户金额是:%.2f元' % money)
elif 500 <= total < 2000:  # lv2
    coupon += 2 * 50
    # 嵌套语句
    answer = input('{}是否要充值(y/n)?'.format(username))
    if answer == 'y':
        money = money + coupon + 1.1 * int(input('输入充值金额:'))
        print('目前账户金额是:%.2f元' % money)
elif total >= 2000:
    coupon += 2 * 100
    # 嵌套语句
    answer = input('{}是否要充值(y/n)?'.format(username))
    if answer == 'y':
        money = money + coupon + 1.2 * int(input('输入充值金额:'))
        print('目前账户金额是:%.2f元' % money)
else:
    print('输入有误')
    1.5 if 注意事项
        区间比较:
        三元运算符(代码简单情况下):变量=值1 if 条件 else 值2---->条件成立,将值1赋值给变量,否则将值2赋值给变量
        自动转换类型:如果if判断输入不是布尔值,在代码执行过程中会将值转化为一个布尔值。
                    只有0," ",' ',None,{},(),[]会被转成False,其他都会被转换为Ture

'''
if注意事项
'''
# 三元运算符(代码简单情况下)
a = 10
b = 60
c = a if a > b else b
print(c)
# #自动转换类型
if None:
    print('1111')
else:
    print('0000')
'''
if语句总练习
模拟超市付款
商品单价,商品数量
键盘上输入商品单价,以及商品数量,再计算应付总额,计算总额 float
提示用户可以有4种付款方式:
    现金没有折扣
    微信0.95折
    支付宝 鼓励金付款金额的10%
    刷卡 满100-20
'''
print('------欢迎光临绚丽超市------')
total = 0  # 商品总额
price = float(input('商品单价:'))
number = int(input('商品数量:'))
total = price * number
choice = input('选择付款方式:1.现金\t2.微信\t3.支付宝\t4.刷卡\n')
if choice == '1':
    # 现金没有折扣
    print('原价:%.2f,应付总额:%.2f元' % (total, total))
elif choice == '2':
    # 微信0.95折
    total1 = total * 0.95
    print('原价:%.2f,应付总额:%.2f元' % (total1, total))
elif choice == '3':
    # 支付宝 鼓励金付款金额的10%
    total1 = total - 0.1 * total
    print('原价:%.2f,应付总额:%.2f元' % (total1, total))
elif choice == '4':
    # 刷卡 满100-20
    if total >= 100:
        total1 = total - 20
        print('原价:%.2f,应付总额:%.2f元' % (total1, total))
    else:
        print('没有折扣!应付金额:%.2f元' % total)
else:
    print('输入错误')
2.循环语句
    2.1 while循环
        2.1.1 while
            格式:while 条件(布尔类型条件):
                    要循环执行的代码
        2.1.2 while...else
           
'''
while循环练习
1.打印1-10之间的数字
2.打印1-50之间能被3整除的数字
3.打印1-10累加和
4.超市买东西  允许买多件商品  计算所有商品的总额
5.猜数字:猜对退出,一共猜了几次
6.猜拳游戏:3局2胜
'''
# 练习1
n = 1
while n <= 10:
    print('------>n=%d' % n)
    n+=1
# 练习2
n = 1
while n<=50:
    if n % 3 == 0:
        print('---->',n)
    n += 1

# 练习3
n = 1
sum = 0
while n < 10:
    sum += n
    # print(n)
    n+=1
print('1-10的累加和:',sum)
# 计数器
count = 0
while True:#True放在while后面,while为死循环,但可通过break跳出结构
    print('11111')
    count += 1
    if count == 5:
        break  # 跳出当前的循环结构(while)
print('over----')
# 练习4
total = 0
count = 0
while True:
    # 先买
    price = float(input('价格:'))
    number = int(input('数量:'))
    # 累加
    total += price * number  # 总额
    count += number
    # 判断是否继续购买
    answer = input('当前商品总额是%.2f元,商品数量是%s件,是否继续添加商品(Q表示退出)' % (total, count))
    if answer == 'Q':
        break  # 跳出当前的while循环
print('当前商品总额是%.2f元,商品数量是%s件' % (total, count))
# 练习5
import random

ran1 = random.randint(1, 50)
print(ran1)  # 自检用
count = 0
while True:
    guess = int(input('请输入你猜的值:'))
    count += 1
    if guess == ran1:
        if count == 1:
            print('一次就中,运气爆棚')
        elif 2 <= count < 5:
            print('运气一般!')
        else:
            print('运气不太行!')
        break
    elif guess > ran1:
        print('猜大了!')
    else:
        print('猜小了')
print('一共猜了%s次' % count)
#练习6
import random

n = 1
p_count = 0  # 人赢次数计数
m_count = 0  # 机器
while n <= 3:
    ran = random.randint(0, 2)
    # print(ran)  # 自检时用
    guess = int(input('请输入:剪刀(0),石头(1),布(2)\n'))
    if guess == 0 and ran == 2 or guess == 1 and ran == 0 or guess == 2 and ran == 1:
        print('我赢了!')
        p_count += 1
    elif ran == 0 and guess == 2 or ran == 1 and guess == 0 or ran == 2 and guess == 1:
        print('机器赢了!')
        m_count += 1
    else:
        print('本轮平局!')
    n += 1
if p_count > m_count:
    print('最终人获胜!')
elif p_count < m_count:
    print('最终机器获胜!')
else:
    print('最终平局!')
    2.2 for循环
        2.2.1 for
            格式:for i in range(n):
                    循环体中的内容
            range(n)--->从0到n-1
            range(start,stop-1)
            range(start,stop-1,step)---->step步长
'''
for
1.1-10数字打印
2.1-50的累加和
3.打印正三角
'''
# # 练习1
for i in range(11):
    print(i)
    i += 1
# 练习2
sum = 0
for i in range(1, 51):
    sum += i
    i += 1
    print('---->', i)
print(sum)
# 练习3
n = 1
for i in range(5):
    print('*' * i)
    i += 1
        2.2.2 for....else
            格式:for i in range(n):
                    循环体
                 else:
                    如果上面的for循环从0~n-1没有出现过中断(break)
'''
for...else
练习:最多输入用户名和密码,如果三次没有登录成功,提示账号被锁定
'''
# 练习
count=0
for i in range(3):
    username=input('输入用户名:')
    password=input('输入密码:')
    if username=='admin'and password=='1234':
        print('登录成功!')
        break
    else:
        print('密码或账号输入有误,请重新输入!')
    i+=1
else:
    print('输入超出次数,账号被锁定!')
    2.3 跳出语句:break  continue
        break:跳出循环结构
        continue:跳过本次循环,继续下一次循环
        两者共性:都出现在循环体中
'''
跳出语句:
break
continue
不打印能被3整除的
'''
# # 练习
for i in range(10):
    if i % 3 == 0:
        #continue  # 跳过下方语句不执行,继续下一次循环
        break #跳出总循环,
    print('-->', i)
print('---over---')
    2.4 循环嵌套
        if  条件:
            if 条件:
                pass
        else:
            if 条件:
                pass
        while循环
'''
循环嵌套:
1.打印正三角形
2.倒三角
'''
# 1
# n = 1
# while n <= 5:
#     print('*' * n)
#     n += 1
# 2
n = 1
while n <= 5:#控制行数
    m = 0
    while m < n:
        print('*', end='')#
        m += 1
    n += 1
    print()
# 倒三角
n = 1
while n <= 5:  # 控制行数
    m = 5
    while m >= n:
        print('*', end='')
        m -= 1
    n += 1
    print()
    注意:1.else特点:不被中断才执行,否则不执行
         2.for循环,肯定有固定次数
           while循环,固定次数/不固定次数(while True)循环
'''
while...else
练习:1.打印1-10
'''
n = 1
while n <= 10:
    print('--->', n)
    if n == 5:
        break
    n += 1
else:
    print('---over---')
'''
循环大练习
掷色子:两个  1-6
    1.玩游戏要有金币数,不能玩游戏
    2.玩游戏赠金币1枚,充值获取金币
    3.10元的倍数:20个金币
    4.玩游戏消耗5个金币
    5.猜大小  :猜对,奖励2枚金币,猜错没有奖励
    6.游戏结束:1.主动退出 2.没有金币退出
    7.只要退出则打印金币数,共玩了几局
'''
import random

print('----玩游戏啦----')

# 登录游戏
coins = 0  # 金币数
n = 0
count = 0
if coins < 5:
    # 提示充币
    print('金币数不足,请充值!')
    #
    while True:
        money = int(input('请输入充值金额:'))
        # 10元的倍数,20个金币
        if money % 10 == 0:
            coins += money // 10 * 20
            print('充值成功,当前金币有%s个' % coins)
            # 开启游戏之旅
            print('----开启游戏之旅!----')
            answer = input('是否开始游戏(y/n)?')
            while coins > 5 and answer == 'y':
                # 扣金币
                coins -= 5
                # 增金币
                coins += 1
                # 产生两枚随机的色子数
                ran1 = random.randint(1, 6)
                ran2 = random.randint(1, 6)
                # 猜大小
                guess = input('洗牌完毕,请猜大小:')
                # 比较判断
                if guess == '大' and ran1 + ran2 > 6 or guess == '小' and ran1 + ran2 <= 6:
                    print('恭喜猜对了!')
                    coins += 2
                else:
                    print('很遗憾,本次猜错了!')
                # 玩的次数
                count += 1
                answer = input('是否继续游戏(y/n)?')
            # 打印玩的次数
            print('共玩了%d次,剩余金币:%d' % (count, coins))
            break
        else:
            print('充值失败')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值