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)
2
  • 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. Commented Apr 18, 2017 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<Object> in Java. Commented Apr 18, 2017 at 18:42

4 Answers 4

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

Comments

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: "))

2 Comments

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?"
If you are using python3 you should use input it will return str. Read the docs for better clarification. Or read this
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']))

2 Comments

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.
please update the answer, check the errors in the last line. also add dict implementation or explanation to your answer, will be helpful.
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])

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.