2
studentNumber = len(age) - 1
age[studentNumber] = 0
house[studentNumber] = ''
reactionTime[studentNumber] = 0

while True:
  try:
    age[studentNumber] = int(input("What is the age of the student: "))
  except ValueError:
    age[studentNumber] = int(input("What is the age of the student: "))

  if age[studentNumber] in range(12, 17):
    break

house[studentNumber] = input("Which house is the student in (Saturn/Mars): ").lower()
while house[studentNumber] not in {"saturn", "mars"}:
  house[studentNumber] = input("Which house is the student in (Saturn/Mars): ").lower()

age.append(0)
house.append('')
reactionTime.append(0)

print(age + " " + house + " " + reactionTime)

I'm trying to validate the input as a string that is "saturn"or "mars", but I'm receiving the error TypeError: can only concatenate list (not "str") to list when I try to add it to my list

The command line:

What would you like to do:
1. Enter new information
2. House-based statsitics
3. Specific Criteria statistics
Enter 1 2 or 3:  1
What is the age of the student:  12
Which house is the student in (Saturn/Mars):  saturn
Traceback (most recent call last):
  File "python", line 46, in <module>
  File "python", line 32, in newInfo
TypeError: can only concatenate list (not "str") to list
12
  • 1
    Please post all of your code. None of the code you posted can raise the error you're getting. Commented Oct 2, 2018 at 6:11
  • @Loocid Updated it Commented Oct 2, 2018 at 6:15
  • @YousifAlnajjar Are age, house and reactionTime all lists? In the updated code you have posted they are undefined. Commented Oct 2, 2018 at 6:17
  • @MatthewSmith They're defined as globals Commented Oct 2, 2018 at 6:18
  • 2
    I think age + " " + house + " " + reactionTime is supposed to be age[studentNumber] + " " + house[studentNumber] + " " + reactionTime[studentNumber]? Commented Oct 2, 2018 at 6:18

1 Answer 1

2

age, house and reactionTime are lists and cannot be concatenated with strings.

You should zip the three lists and iterate through the items for output.

Change:

print(age + " " + house + " " + reactionTime)

to:

for a, h, r in zip(age, house, reactionTime):
    print(a, h, r)
Sign up to request clarification or add additional context in comments.

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.