0

I have 3 arrays of size 5, one containing strings of names, another that correlates with their age, and another that correlates with their salary.

This is my first time using Python so I'm used to Java syntax and I'm not sure if I am approaching this right.

I have the following 2 lines:

for i in range(5):
    name = raw_input("Enter a person's name: ")
    nameArray.append(str(name))
    age = raw_input("What is their age: ")
    ageArray.append(int(age))
    salary = raw_input("How much do they make: ")
    salaryArray.append(int(salary))

for j in range(5):
    print(nameArray + ' is ' + ageArray + ' years old and makes $' + salary)
zerrisk
  • 33
  • 1
  • 7
  • you need to use `index`, `nameArray[j]` and so on... and you are using python2 or python3. I have a doubt about `raw_input()` method. – bhansa Apr 18 '17 at 18:21
  • You are almost certainly using *lists* not arrays. In python, "array" should be used to refer specifically to either `array.array` or `numpy.array` types, not lists. While you certainly see sloppy use of the term, it is best to be precise. Python lists are very different than Java arrays, they are more like `ArrayList` in Java. – juanpa.arrivillaga Apr 18 '17 at 18:42

4 Answers4

2

You need to care about lists almost same as arrays, and you need to provide index if you are accessing elements in any particular list same you do with arrays in java.

Updated Code:

nameArray = []
ageArray = []
salaryArray = []
for i in range(5):
    name = input("Enter a person's name: ")
    nameArray.append(str(name))
    age = input("What is their age: ")
    ageArray.append(int(age))
    salary = input("How much do they make: ")
    salaryArray.append(int(salary))

for j in range(5):
    print(nameArray[j] + ' is ' + str(ageArray[j]) + ' years old and makes $' + str(salaryArray[j]))

The Byte of Python Nice book for a fresh start.

If you are using python2 and just want to print the results without storing :-)

for i in range(5):
    print "{} is {} years old and makes {}".format(raw_input("Enter a person's name: "),raw_input("What is their age: "),raw_input("How much do they make: "))
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • I'm getting a traceback and name error for the line where I am setting the name and asking for a input for the name. Would it be due to me changing raw_input to input? I'm reading online that raw_input returns a string and input returns an "expression?" – zerrisk Apr 18 '17 at 19:06
  • If you are using python3 you should use `input` it will return str. Read the docs for better clarification. Or read [this](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) – bhansa Apr 19 '17 at 03:33
2

Use dictionaries to reflect your interest in employees that have names, ages, and salaries, rather than lists that are implicitly related.

employees = []
for i in range(5):
    name = raw_input("Enter a person's name: ")
    age = raw_input("What is their age: ")
    salary = raw_input("How much do they make: ")
    employees.append({"name": name, "age": age, "salary": salary})

for emp in employees:
    print('{name} is {age} years old and makes ${salary}'.format(**emp))
chepner
  • 497,756
  • 71
  • 530
  • 681
0

welcome to python. It is a wonderfull rabit-hole :)

what you are trying to do works (with a few tweaks) but is not the most pythonic approach firstly your attempt: use input rather than raw_input and secondly, you need to define your arrays (called lists in python) before you can append

e.g.

nameArray = []
ageArray = []
salaryArray = []

for i in range(5):
    name = input("Enter a person's name: ")
    nameArray.append(str(name))
    age = input("What is their age: ")
    ageArray.append(int(age))
    salary = input("How much do they make: ")
    salaryArray.append(int(salary))

for j in range(5):
    print(nameArray[j] + ' is ' + ageArray[j] + ' years old and makes $' + salaryArray[j])

however, using a dict is better (aka more pythonic). see the example below:

persons = []
for i in range(5):
    name   = input("Enter a person's name: ")
    age    = input("What is their age: ")
    salary = input("How much do they make: ")
    person = {'name' : name, 'age' : int(age), 'salary' : int(salary)}
    persons.append(person)

for person in persons:
    print(person['name'], 'is', str(person['age']), 'years old and makes $', str(person['salary']))
Marc Wagner
  • 1,672
  • 2
  • 12
  • 15
  • 1
    For this case, however, I would suggest a dict. e.g. person = {'name' : n, 'age' : a, 'salary' : s} and then append person to a list of persons. This ensures that the data stays together. with 3 seperate arrays it is possible that one or more entries in an array are added or deleted without updating any of the others. – Marc Wagner Apr 18 '17 at 18:35
  • please update the answer, check the errors in the last line. also add dict implementation or explanation to your answer, will be helpful. – bhansa Apr 18 '17 at 18:35
0

according to me this code will helpful for solving your problem:-

nameArray=[]
ageArray=[]
salaryArray=[]
for i in range(2):
    name = raw_input("Enter a person's name: ")
    nameArray.append(name)
    age = raw_input("What is their age: ")
    ageArray.append(age)
    salary =raw_input("How much do they make: ")
    salaryArray.append(salary)

for j in range(2):
    print(nameArray[j] + ' is ' + ageArray[j] + ' years old and makes $' + salaryArray[j])
Rohit-Pandey
  • 2,039
  • 17
  • 24