learn python the hard way (personal) -- Ex27-

本文深入探讨了Python中的控制流程概念,包括if语句、if-else语句的嵌套使用,以及for和while循环的实践应用。通过具体的代码示例,讲解了条件判断和循环控制的语法与逻辑,为初学者提供了丰富的编程实践指导。

Ex27:

一系列逻辑练习,类似于离散数学中的逻辑部分,所以跳过。

 

Ex28:

仍然是逻辑练习部分

 

Ex29:

if语句

people = 20
cats = 30
dogs = 15

if people < cats:
    print("Too many cats! The world is doomed!")

 

Ex30:

if-else语句:

people = 30
cars = 40
trucks = 15

if cars > people:
    print("We should take the cars.")
elif cars < people:
    print("We should not take the cars.")
else:
    print("We can't decide.")

What happens if multiple elif blocks are True? Python starts at the top and runs the frst block that is
True, so it will run only the frst one.
 

Ex31:

if-else语句的嵌套使用

elif door == "2":
        print("You stare into the endless abyss at Cthulhu's retina.")
        print("1. Blueberries.")
        print("2. Yellow jacket clothespins.")
        print("3. understanding revolvers yelling melodies.")

        insanity = input(">")

        if  insanity == "1" or insanity == "2":
            print("Your body survives powered by a mind of jello.")
            print("Good job!")
        else:
            print("The insanity rots your eyes into a pool of muck.")
            print("Good job!")

How do I tell whether a number is between a range of numbers? You have two options: Use 0 < x <
10 or 1 <= x < 10, which is classic notation, or use x in range(1, 10).
注意是 insanity == "1"

Ex32:

for循环 及 list:

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'diems', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print(f"This is count: {number}")

# same as above
for fruit in fruits:
    print(f"A fruit of type: {fruit}")

# also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it
for i in change:
    print(f"I got {i}")

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0,6):
    print(f"Adding {i} to the list.")
    # append is a function that lists understand
    elements.append(i)

Common Student Questions


How do you make a 2-dimensional (2D) list? That’s a list in a list like this: [[1,2,3],[4,5,6]]


Aren’t lists and arrays the same thing? Depends on the language and the implementation. In classic
terms a list is very different from an array because of how they’re implemented. In Ruby though
they call these arrays. In Python they call them lists. Just call these lists for now since that’s what
Python calls them.


Why is a for-loop able to use a variable that isn’t defined yet? The variable is defned by the for-loop
when it starts, initializing it to the current element of the loop iteration each time through.


Why does for i in range(1, 3): only loop two times instead of three times? The range() function
only does numbers from the frst to the last, not including the last. So it stops at two, not three
in the preceding. This turns out to be the most common way to do this kind of loop.


What does elements.append() do? It simply appends to the end of the list. Open up the Python shell
and try a few examples with a list you make. Any time you run into things like this, always try to
play with them interactively in the Python shell.
 

Ex33:

while循环:

i = 0
numbers = []

while i < 6 :
    print(f"At the top i is {i}")
    numbers.append(i)

    i += 1
    print("Numers now:", numbers)
    print(f"At the bottom i is {i}")

用for循环来实现上面的while循环。

 

多角色体系 支持管理员、商家、消费者三种角色,权限分级管控: 管理员:负责平台整体配置、用户审核、数据监控等全局操作。 商家:管理店铺信息、发布商品、处理订单、回复评价等。 消费者:浏览商品、加入购物车、下单支付、评价商品等。 实现用户注册(手机号 / 邮箱验证)、登录(支持密码 / 验证码 / 第三方登录)、个人信息管理(头像、收货地址、密码修改)。 权限精细化控制 商家仅能管理自家店铺及商品,消费者仅能查看和购买商品,管理员拥有全平台数据访问权限。 二、商品管理功能 商品信息维护 商家可发布商品:填写名称、分类(如服饰、电子产品)、子类别(如手机、笔记本)、规格(尺寸、颜色、型号)、价格、库存、详情描述(图文)、物流信息(运费、发货地)等。 支持商品上下架、库存调整、信息编辑,系统自动记录商品状态变更日志。 商品分类与搜索 按多级分类展示商品(如 “数码产品→手机→智能手机”),支持自定义分类体系。 提供智能搜索功能:按关键词(名称、品牌)搜索,支持模糊匹配和搜索联想;结合用户浏览历史对搜索结果排序(优先展示高相关度商品)。 商品推荐 基于用户浏览、收藏、购买记录,推荐相似商品(如 “浏览过该商品的用户还买了…”)。 首页展示热门商品(销量 TOP10)、新品上架、限时折扣等推荐列表。 三、订单与交易管理 购物车与下单 消费者可将商品加入购物车,支持修改数量、选择规格、移除商品,系统自动计算总价(含运费、折扣)。 下单流程:确认收货地址→选择支付方式(在线支付、货到付款)→提交订单→系统生成唯一订单号。 订单处理流程 订单状态跟踪:待支付→已支付→商家发货→物流运输→消费者收货→订单完成,各状态变更实时通知用户。 商家端功能:查看新订单提醒、确认发货(填写物流单号)、处理退款申请(需审核理由)。 消费者端功能:查看订单详情、追踪物流、申请退款 / 退货、确认收货。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值