0

So, I have a python script which requires an input from the terminal. I have 20 different arrays and I want to print the array based on the input.

This is the code minus the different arrays.

homeTeam = raw_input()

awayTeam = raw_input()

a = (homeTeam[0])+(awayTeam[3])/2
b = (hometeam[1])+(awayTeam[2])/2

So, effectively what I want to happen is that homeTeam/awayTeam will take the data of the array that is typed.

Thanks

idjaw
  • 25,487
  • 7
  • 64
  • 83

3 Answers3

1

You may take input as the comma separated (or anything unique you like) string. And call split on that unique identifier to get list (In Python array and list are different).

Below is the example:

>>> my_string = raw_input()
a, b, c, d
>>> my_list = my_string.split(', ')
>>> my_list
['a', 'b', 'c', 'd']

Since you are having your list now, you already know what you need to do with it.

Alternatively, you may also extract list from raw_input by using eval. But it is highly recommended not to use eval. Read: Is using eval in Python a bad practice?

Below is the example:

>>> my_list = eval(raw_input())
[1, 2, 3, 4, 5]
>>> my_list[2]
3
Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You can take raw_input() as string and then you can use split function to make it array. While doing arithmetic stuff you need to do type casting. for example,

homeTeam = raw_input() ### 1,2,3,4
homeTeam = homeTeam.split(",")

awayTeam = raw_input() ### 5,6,7,8
awayTeam = awayTeam.split(",")

a = (int(homeTeam[0]) + int(awayTeam[3]))/2
b = (int(hometeam[1]) + int(awayTeam[2]))/2
yasirnazir
  • 1,133
  • 7
  • 12
0

Instead of 20 individual arrays you could use a dictionary.

d = {"team1": "someValue",
     "team2": "anotherValue",
     ...
}

Then you can retrieve the values of a team by its name:

x = raw_input("team1")

d[x] will now return "someValue".

In your particular case, you can use arrays for the values of the dictionary, for example:

d = {"team1": [value1, value2, ...],
     "team2": [...],          
     ...
}

Now d[x] returns the array [value1, value2, ...].

Complete example

Finally, you could wrap all of this into a single function f:

def f():
    homeTeam = d[raw_input("Enter home team: ")]
    awayTeam = d[raw_input("Enter away team: ")]
    a = (homeTeam[0] + awayTeam[3])/2
    b = (homeTeam[1] + awayTeam[2])/2
    return a, b

By calling f() the user will be prompted for two team names. And the function will return the values of both teams in form of a tuple.

imant
  • 597
  • 5
  • 15