How to display two different return values from if/elif in Python?

Question:

I’m reading a csv file and appending the data into a list and later using another function, I’m calculating these numbers and try to return two values using if/elif statements. To display the result I have created a procedure called displayData(numbers) and here I’m struggling to show the calculated values from previous function called seyrogus(numbers) against the original csv values.
Currently, it’s showing all only "4" as result. Where I’m doing wrong?

My code so far

def getData():

  numbers = []

  file = open("numbers.csv","r")

  for line in file:

    data = line.strip()

    numbers.append(int(data.strip()))
  
  return numbers


def seyrogus(numbers):

  for counter in range(len(numbers)):
    if numbers[counter] % 2 != 0:
      return (int(numbers[counter] * 3) + 1)

    elif numbers[counter] % 2 == 0:
      return (int(numbers[counter] / 2))

def displayData(numbers):

  print("Original Numbers t Converted Numbers")

  for counter in range(len(numbers)):

    print(f"{numbers[counter]} t t t {seyrogus(numbers)}")
  
def main():

  numbers = getData()
  displayData(numbers)
 

main()

output

Original Numbers         Converted Numbers
1                        4
2                        4
3                        4
4                        4
5                        4
6                        4
7                        4
8                        4
9                        4
10                       4
11                       4
12                       4
13                       4
14                       4
15                       4
16                       4
17                       4
18                       4
19                       4
20                       4
21                       4
22                       4
23                       4
24                       4
25                       4
26                       4
27                       4
28                       4
29                       4
30                       4
31                       4
32                       4
33                       4
34                       4
35                       4
36                       4
37                       4
38                       4
39                       4
40                       4
41                       4
42                       4
43                       4
44                       4
45                       4
46                       4
47                       4
48                       4
49                       4
50                       4
51                       4
52                       4
53                       4
54                       4
55                       4
56                       4
57                       4
58                       4
59                       4
60                       4
61                       4
62                       4
63                       4
64                       4
65                       4
66                       4
67                       4
68                       4
69                       4
70                       4
71                       4
72                       4
73                       4
74                       4
75                       4
76                       4
77                       4
78                       4
79                       4
80                       4
81                       4
82                       4
83                       4
84                       4
85                       4
86                       4
87                       4
88                       4
89                       4
90                       4
91                       4
92                       4
93                       4
94                       4
95                       4
96                       4
97                       4
98                       4
99                       4
100                      4

csv file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Asked By: Mateo

||

Answers:

Your seyrogus function needs to return a list rather than returning a single value. The reason you’re only getting 4 as the result is that every time you call it, it iterates over numbers from the beginning and then returns the first converted value rather than iterating over the entire list.

Both getData and seyrogus can be implemented very simply as list comprehensions. You then need to iterate over both numbers and seyrogus(numbers) in parallel in displayData; an easy way of doing that is the zip function.

def getData():
    with open("numbers.csv") as file:
        return [int(line.strip()) for line in file]

def seyrogus(numbers):
    return [n * 3 + 1 if n % 2 else n // 2 for n in numbers]

def displayData(numbers):
    print("Original Numbers t Converted Numbers")
    for original, converted in zip(numbers, seyrogus(numbers)):
        print(f"{original} t t t {converted}")

def main():
    displayData(getData())

main()

prints:

Original Numbers         Converted Numbers
1                        4
2                        1
3                        10
4                        2
5                        16
6                        3
7                        22
8                        4
9                        28
10                       5
11                       34

etc.

Answered By: Samwise
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.