-3

I want to make a discount of 31% to all users under 25 years and 50% to users over 40 years.

On the other side to users who do not live in Madrid only a discount of 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}')

Print Example: Name: Marcos - Debt: 586,5

Name: Elena - Debt: 360

Name: Carmen - Debt: 47,5

Name: Carlos - Debt: 290

Name: Maria - Debt: 150

Eduardo
  • 23
  • 3
  • 1
    What's your question? – ForceBru Feb 17 '19 at 13:25
  • How can I print the student's name with the desired discount? Based on these: 1) I want to make a discount of 31% to all users under 25 years and 50% to users over 40 years. 2) On the other side to users who do not live in Madrid only a discount of 5%. With the following format: print(f'Name: {student} - Debt: {debt}') – Eduardo Feb 17 '19 at 13:30
  • 1
    At least give it a try before asking. SO can help, but should not do your homework... Hint: if/elif/else is a first approach. – FabienP Feb 17 '19 at 13:30
  • You're right, I already edit my question to be more clearly. I tried with the previous code, but as a result I get: Syntax error: invalid syntax – Eduardo Feb 17 '19 at 13:50
  • 1
    Your updated answer is not correctly formatted. – ohlr Feb 17 '19 at 13:51

3 Answers3

0

You would have to do something like the following:

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

however, I really recommend you improve your data first. With that I mean instead of using a list I would use a dictionary. maybe even dictionary in dictionary. This way you could use keys such as name or id to acces the data. In the code above i had to use numbers - which is hard to read.

Here a link to the documentation for using dictionaries.


Regarding your updated code

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}')
ohlr
  • 1,839
  • 1
  • 13
  • 29
  • note: you have to have python 3.6 to use `f-strings` in print statements. https://stackoverflow.com/a/19457247/9177173 – ohlr Feb 17 '19 at 14:10
0

The code below uses 'Student' as a namedtuple which is light weight data structure.
It declares list of Student named 'students'. It runs over this list and populate a new list named 'after_discount_students' with new Student object (after debt field is recalculated).

Any questions?

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)

Output:

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')
balderman
  • 22,927
  • 7
  • 34
  • 52
  • this is clearly a much nicer way to do things. However, I guess Eduardo is just learning python. Don't know if this does not introduce to many things unexplained. – ohlr Feb 17 '19 at 13:58
  • Yes you're right, I'm learning Python... Sorry for not being so explicit. – Eduardo Feb 17 '19 at 15:25
  • Eduardo. I have added comments before the code. Feel free to ask questions :-) – balderman Feb 17 '19 at 15:36
0
    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}')
Eduardo
  • 23
  • 3