+ 1
Using a string in a 1D array
I am trying to get the piece of code to work below. Every time I try to enter the students name it comes up with an error. It does allow me to enter numbers names_list = [] marks_list = [] for student in range(5): name = input("Enter students name ") mark = int(input("Enter students mark ")) #add the data to the list names_list.append(name) marks_list.append(mark) print "Student \t Mark" for pupil in range(5): print (names_list[pupil], "\t " , marks_list[pupil])
6 odpowiedzi
+ 2
names_list = []
marks_list = []
for student in range(5):
name = input("Enter students name ")
mark = int(input("Enter students mark "))
#add the data to the list
names_list.append(name)
marks_list.append(mark)
print ("Student \t Mark")
for pupil in range(5):
print (names_list[pupil], "\t " , marks_list[pupil])
Brian it is working you need only add separately per line like this below. I only added parentheses for print
student1
10
student2
9
student3
6
student4
4
student5
2
+ 1
If you're doing Python 2, the:
print(names_list[pupil], "\t", marks_list[pupil])
is incorrect syntax for Python 2, the () is for Python 3.
If you're doing Python 3, the:
print "Student \t Mark"
is incorrect syntax for Python 3, the "" is for Python 2.
+ 1
Oh, in that case, the only issues I can see are the print function calls. The last 3 lines should be:
print("Student \t Mark")
for pupil in range(5):
print(names_list[pupil] + "\t" + marks_list[pupil])
Notice the parentheses around the first print statement's text, and the "+" signs instead of the "," signs in the second one.
Happy coding! 😁
+ 1
Where did the 'Brian' come from? Can you put the code in a code playground code and post here? Thanks! :D
0
Hi thanks for your reply- it is python 3 using pyscripter.
0
Hi Niawahta, still getting NameError name ‘Brian’ is not defined