Skip to main content
  1. About
  2. For Teams
Asked
Viewed 313 times
0

I am very new to 2D Arrays in python. I am trying to create a 2D array that asks the user to input a name and then finds the name in the array and prints out what position that name is.

My code so far is:

pupil_name = [['Jess', 0], ['Tom', 1], ['Erik', 2]]

enter_pupil = input('Enter name of Pupil   ')  
print(str(pupil_name) + ' is sitting on chair number ' + str([]))

print(' ')

Is what I am asking possible? It is just for fun and would love to make it work. Thanks in advance

2
  • 3
    You probably would much rather be using dictionaries for this problem
    Carlo Mazzaferro
    –  Carlo Mazzaferro
    2018-03-15 16:17:21 +00:00
    Commented Mar 15, 2018 at 16:17
  • If you still want to use list of list, You can do so for pupil in pupil_name: if pupil[0] == enter_pupil: print(pupil[1])
    dnp1204
    –  dnp1204
    2018-03-15 16:20:05 +00:00
    Commented Mar 15, 2018 at 16:20

4 Answers 4

2

You should use a dictionary, as others pointed out. However, if you still want to keep a 2D list, here is what you can do:

pupil_name = [['Jess', 0], ['Tom', 1], ['Erik', 2]]
enter_pupil = input('Enter name of Pupil   ')

for pupil, seat in pupil_name:
    if pupil == enter_pupil:
        print('{} is seating at chair number {}'.format(pupil, seat))
        break
else:
    print('Not found: {}'.format(enter_pupil))

Notes

  • The code loops through pupil_name and each iteration assigned the sub list to pupil and seat.
  • If we found a match, we print out the name and seat and break out of the loop. There is no point to keep looping since we found what we want.
  • The else clause is an interesting and unique aspect of Python for loop: if we loop through all names/seats and did not break (i.e. did not find the name), then the code under the else clause is executed.
Sign up to request clarification or add additional context in comments.

Comments

0

You want a dictionary.

>>> pupil_name = [['Jess', 0], ['Tom', 1], ['Erik', 2]]
>>> pupil_pos = dict(pupil_name)
>>> 
>>> pupil_pos
{'Jess': 0, 'Erik': 2, 'Tom': 1}
>>> pupil_pos['Erik']
2

This gives you a mapping of names to positions, which you can query by providing the name.

Comments

0

As others have advised in comments you should use a dict.

You should also be using raw_input instead of input as it converts the user input to a str.

student_chair_numbers = {
    'Jess': 0,
    'Tom': 1,
    'Erik': 2,
}
enter_pupil = raw_input('Enter name of Pupil   ')
print(enter_pupil + ' is sitting on chair number ' + str(student_chair_numbers[enter_pupil]))

Comments

0

Here's a solution that uses your nested lists. See Jim Wright's answer for a dict solution.

pupil_name = [['Jess', 0], ['Tom', 1], ['Erik', 2]]

def find_chair(name, chair_numbers):
    for (n, c) in chair_numbers:
        if n == name:
            return c
    return None

enter_pupil = input('Enter name of Pupil   ')  
print(str(enter_pupil) + ' is sitting on chair number ' + str(find_chair(enter_pupil, pupil_name)))

print(' ')

Comments

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.