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

4
  • 2
    Where are the arrays? Commented Oct 4, 2016 at 12:42
  • 1
    Please provide details on how exactly these arrays look like. What is the expected output of your code? Explain exactly what is happening now that is not meeting your expectations? Ultimately, you want to ensure you are putting together a good minimal reproducible example of your problem Commented Oct 4, 2016 at 12:42
  • take a look at : stackoverflow.com/questions/7845165/… Commented Oct 4, 2016 at 12:56
  • An minimal reproducible example would be helpful. Commented Oct 5, 2016 at 17:00

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.