python学习笔记8(if语句)

1.判断数值相等,不等

#if语句
colors=['red','black','white','green']
print(colors)
for color in colors[:]:
    # if后面的冒号是英文状态下面的,是不可以省略的,比较数值相等用的是‘==’,不是‘=’,不相等用‘!=’,在python中cat和CAT是不同的,是区分大小写的
    if color=='green':
        print('I like '+color.title())#有缩进,执行多次
    else:
        print(color.title()+' is not my favoraite color')
    print('一个元素对比结束')#没有缩进,比else高一层次,每一个元素对比完之后就执行一次
print('循环结束')
['red', 'black', 'white', 'green']
Red is not my favoraite color
一个元素对比结束
Black is not my favoraite color
一个元素对比结束
White is not my favoraite color
一个元素对比结束
I like Green
一个元素对比结束
循环结束

2.判断非空

 if color:#说明对象非空
        print('I like '+color.title())#有缩进,执行多次
#判断列表非空
colors=['red','black','white','green']
if colors:
    print('not null')

3.判断某元素是否存在于列表中

colors=['red','black','white','green']
color='resd'
if color not in colors:#判断对象不在列表中
    print(color.title()+' is not in the list')
color='red'
if color  in colors:#判断对象在列表中
    print(color.title()+' is in the list')
Resd is not in the list
Red is in the list

4.奶茶点单小程序

#奶茶点单小程序,由于目前所学较少,只能点一类奶茶,后期还会完善
print('*****************')
print('欢迎光临波波奶茶店,本店提供以下饮品:\n\t1.红豆相思奶茶  10元/小杯  12元/中杯  15元/大杯\n\t2.椰果珍珠奶茶  10元/小杯  12元/中杯  14元/大杯\n\t3.草莓西瓜果汁  10元/小杯  11元/中杯  12元/大杯\n\t')
milktea_number=input('请输入您想要的奶茶编号:')
if milktea_number:
    milktea_number=int(milktea_number)#如果用户输入奶茶编号,就将编号记录下来
else:
    milktea_number=0#默认为0,代表不点单
milktea_numbers=[]
milktea_numbers.append(milktea_number)
price=0#price参与后期计算,初值定义为0
for milktea_number in milktea_numbers[:]:
    if milktea_number >= 1 and milktea_number <= 3:  #注意这里的冒号(英文状态下)是不能省略的
        if milktea_number == 1:
            milktea_name = '红豆相思奶茶'
            milktea_size = input('请输入{}号{}的规格(小杯、中杯、大杯)'.format(milktea_number,milktea_name))
            if milktea_size:
                milktea_size =milktea_size # 如果用户输入奶茶规格,就将规格记录下来
            else:
                milktea_size='大杯'#默认大杯
            if milktea_size == '小杯':
                milktea_price = 10#小杯价格
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            elif milktea_size == '中杯':
                milktea_price = 12
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            else:
                milktea_price = 15
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
        if milktea_number == 2:
            milktea_size = '大杯'  # 默认大杯
            milktea_name = '椰果珍珠奶茶'
            milktea_size = input('请输入{}号{}的规格(小杯、中杯、大杯)'.format(milktea_number, milktea_name))
            if milktea_size:
                milktea_size =milktea_size # 如果用户输入奶茶规格,就将规格记录下来
            else:
                milktea_size='大杯'#默认大杯
            if milktea_size == '小杯':
                milktea_price = 10
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            elif milktea_size == '中杯':
                milktea_price = 12
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            else:
                milktea_price = 14
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
        if milktea_number == 3:
            milktea_size = '大杯'  # 默认大杯
            milktea_name = '草莓西瓜汁'
            milktea_size = input('请输入{}号{}的规格(小杯、中杯、大杯)'.format(milktea_number, milktea_name))
            if milktea_size:
                milktea_size =milktea_size # 如果用户输入奶茶规格,就将规格记录下来
            else:
                milktea_size='大杯'#默认大杯
            if milktea_size == '小杯':
                milktea_price = 10
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            elif milktea_size == '中杯':
                milktea_price = 11
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
            else:
                milktea_price = 12
                milktea_quantity = input('请输入奶茶数量:')
                if milktea_quantity:
                    milktea_quantity = int(milktea_quantity)  # 如果用户输入奶茶数量,就将数量记录下来
                else:
                    milktea_quantity = 0  # 默认0杯
                price += milktea_price * milktea_quantity
        vip = input('是否为本店会员y/n:')
        if vip:
            vip=vip
        else:
            vip='n'#默认不是会员
        if vip == 'y':
            price1 = price * 0.9
            print(
                '小主,您点了{}号{},共{}杯{}奶茶,原计{}元,折后{}元'.format(milktea_number, milktea_name, milktea_quantity, milktea_size,price, price1))
        else:
            price = price
            print(
                '小主,您点了{}号{},共{}杯{}奶茶,共{}元'.format(milktea_number, milktea_name, milktea_quantity, milktea_size, price))
            print('小主您的奶茶已经下单,请耐心等待~')
    else:
        print('抱歉小主,您点的奶茶本店暂时缺货,欢迎品尝其他口味的奶茶')

你可能感兴趣的:(python,python)