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)
入力を「saturn」または「mars」の文字列として検証しようとしていますTypeError: can only concatenate list (not "str") to list
が、リストに追加しようとするとエラーが発生します。
コマンドライン:
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
age
、house
およびreactionTime
はリストであり、文字列と連結することはできません。
3つのリストを圧縮し、出力する項目を繰り返し処理する必要があります。
変化する:
print(age + " " + house + " " + reactionTime)
に:
for a, h, r in zip(age, house, reactionTime):
print(a, h, r)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加