【问题标题】:Make a discount to certain users in a list对列表中的某些用户进行折扣
【发布时间】:2019-07-11 00:51:33
【问题描述】:

我想为 25 岁以下的所有用户提供 31% 的折扣,为 40 岁以上的用户提供 50% 的折扣。

另一方面给不住在马德里的用户只有5%的折扣。

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'Madrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'Madrid', 580, '00887118456'),
    ('Maria', 28, 'Madrid', 150, '587')
]

for item in students:
    student, age, city, debt, id = item
if age < 25
    else debt * 0.69
if age > 40
    else debt * 0.5
if city is not 'Madrid'
    else debt * 0.95
    print(f'Name: {student} - Debt: {debt}')

打印示例: 姓名:马科斯 - 债务:586,5

姓名:Elena - 债务:360

姓名:卡门 - 债务:47,5

姓名:卡洛斯 - 债务:290

姓名:玛丽亚 - 债务:150

【问题讨论】:

  • 你有什么问题?
  • 我如何打印学生的姓名并获得所需的折扣?基于这些: 1)我想为所有 25 岁以下的用户提供 31% 的折扣,为 40 岁以上的用户提供 50% 的折扣。 2) 另一边给不住在马德里的用户只有5%的折扣。使用以下格式: print(f'Name: {student} - Debt: {debt}')
  • 在询问之前至少试一试。 SO 可以提供帮助,但不应该做你的功课......提示:if/elif/else 是第一种方法。
  • 你说得对,我已经将我的问题编辑得更清楚了。我尝试使用以前的代码,但结果我得到:语法错误:无效语法
  • 您更新的答案格式不正确。

标签: python


【解决方案1】:

您必须执行以下操作:

for item in students:
    # check if age < 25
    if(item[1] < 25):
           #add discount of 31%
           item[3] = item[3] * (1-0.31)

但是,我真的建议您首先改进您的数据。 我的意思是我不会使用列表,而是使用字典。甚至可能是字典中的字典。这样您就可以使用诸如 nameid 之类的键来访问数据。在上面的代码中,我不得不使用数字——这很难阅读。

这里是documentation 的链接,用于使用字典。


关于您更新的代码

for item in students:
    student, age, city, debt, id = item
    #check age
    if age < 25:
        debt *= (1-0.31)
    elif age > 40:
        debt *= 0.5

    print(f'Name: {student} - Debt: {debt}')

【讨论】:

【解决方案2】:

下面的代码使用“Student”作为namedtuple,这是一种轻量级数据结构。
它声明了名为“学生”的学生列表。 它遍历这个列表并用新的 Student 对象填充一个名为“after_discount_students”的新列表(在重新计算债务字段之后)。

有什么问题吗?

import collections

Student = collections.namedtuple('Student', 'name age city debt id')

students = [
    Student('Marcos', 23, 'Madrid', 850, '2388711341'),
    Student('Elena', 35, 'MaDrid', 360, '0387700342'),
    Student('Carmen', 21, 'Getafe', 50, '0014871388'),
    Student('Carlos', 41, 'MAdrid', 580, '00887118456'),
    Student('Maria', 28, 'Madrixx', 150, '587')
]

after_discount_students = []


def discount_calc(student):
    if student.age < 25:
        return 0.69
     elif student.age > 40:
        return 0.5
     else:
        return 1


for student in students:
    after_discount_students.append(
        Student(student.name, student.age, student.city, discount_calc(student) * 
                student.debt, student.id))
for student in after_discount_students:
    print(student)

输出:

Student(name='Marcos', age=23, city='Madrid', debt=586.5, id='2388711341')
Student(name='Elena', age=35, city='MaDrid', debt=360, id='0387700342')
Student(name='Carmen', age=21, city='Getafe', debt=34.5, id='0014871388')
Student(name='Carlos', age=41, city='MAdrid', debt=290.0, id='00887118456')
Student(name='Maria', age=28, city='Madrixx', debt=150, id='587')

【讨论】:

  • 这显然是一种更好的做事方式。但是,我猜 Eduardo 只是在学习 python。不知道这样是不是引入了很多无法解释的东西。
  • 是的,你是对的,我正在学习 Python... 抱歉没有这么明确。
  • 爱德华多。我在代码之前添加了 cmets。随意提问:-)
【解决方案3】:
    students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'Madrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'Madrid', 580, '00887118456'),
    ('Maria', 28, 'Madrid', 150, '587')
]

for item in students :
    student, age, city, debt, id = item
    if age < 25 :
        debt *= (1-0.31)
        print (f'Student: {student} - Debt: {debt}')
    elif age > 40 :
        debt *= (1-0.5)
        print (f'Student: {student} - Debt: {debt}')
    elif city.startswith('G') :
        debt *= (1-0.05)
        print (f'Student: {student} - Debt: {debt}')
    else :
        print (f'Student: {student} - Debt: {debt}')

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多