0

Summary: Program takes in 5 inputs from the user (1 string and 4 integers). For the exercise, I'm doing we need to convert the placing into points e.g. 1st place = 5pts, 2nd place = 3pts, 3rd place = 1pts, anything else = 0pts. Then the result will be printed out.

def calc_points(points):
    for i in range(len(points)): 
        if points[i] == 1:
            points[i] = 5
        elif points[i] == 2:
            points[i] = 3
        elif points[i] == 3:
            points[i] = 1
        else:
            points[i] = 0
    return points
        
def get_place():

    place = []

    place.append(int(input("What place did you come in for your first race?\n> ")))
    place.append(int(input("What place did you come in for your second race?\n> ")))
    place.append(int(input("What place did you come in for your third race?\n> ")))
    place.append(int(input("What place did you come in for your fourth race?\n> ")))
    return place

def get_rider_info():
    rider_info = []
    rider_info.append(input("What is the Rider's Name?\n> "))

    points = get_place()

    total_points = sum(calc_points(points))
    points.append(total_points)

    rider_info.extend(points)

    return rider_info
        

rider1 = get_rider_info()
print(rider1)

The problem is when the calc_points() is executed the list points changes e.g. input: Bob, 1, 2, 3, 4 Output: ['Bob', 5, 3, 1, 0, 9] when I want it to be ['Bob', 1, 2, 3, 4, 9].

Soupkiller
  • 11
  • 1

3 Answers3

1

In python when you pass a list, the list is not copied; you are passing the original list object itself, hence the actual elements themselves gets altered. Instead, you can create a new list in your calc_points like:

def calc_points(points):
    L=[]
    for i in range(len(points)): 
        if points[i] == 1:
            L.append(5)
        elif points[i] == 2:
            L.append(3)
        elif points[i] == 3:
            L.append(1)
        else:
            L.append(0)
    return L
  • heh. I just posted the same response. I'll leave mine up because I mentioned that calling the array of rankings "points" is also a bad thing to do. – toppk Sep 03 '22 at 22:29
0

Because your list has only immutable values (integers are immutable), to do a copy, you can just use a slice which selects all the elements of the list:

def calc_points(points):
    points = points[:]
    for i in range(len(points)): 
        if points[i] == 1:
            points[i] = 5
        elif points[i] == 2:
            points[i] = 3
        elif points[i] == 3:
            points[i] = 1
        else:
            points[i] = 0
    return points
        
def get_place():

    place = []

    place.append(int(input("What place did you come in for your first race?\n> ")))
    place.append(int(input("What place did you come in for your second race?\n> ")))
    place.append(int(input("What place did you come in for your third race?\n> ")))
    place.append(int(input("What place did you come in for your fourth race?\n> ")))
    return place

def get_rider_info():
    rider_info = []
    rider_info.append(input("What is the Rider's Name?\n> "))

    points = get_place()

    total_points = sum(calc_points(points))
    points.append(total_points)

    rider_info.extend(points)

    return rider_info
        

rider1 = get_rider_info()
print(rider1)

If you want to learn more about this behavior, see this question.

pigrammer
  • 2,603
  • 1
  • 11
  • 24
0

While the above solutions will produce expected results, the issue is that you are confusing ranks and points. It is better to create a separate array for the points, and use a different name for the ranks. Here is a cleaned up calc_points method.

def calc_points(ranks):
    points = []
    for rank in ranks: 
        if rank == 1:
            points.append(5)
        elif rank == 2:
            points.append(3)
        elif rank == 3:
            points.append(1)
        else:
            points.append(0)
    return points
toppk
  • 696
  • 4
  • 10