H
H
Heonir2021-06-14 15:41:03
Python
Heonir, 2021-06-14 15:41:03

What is wrong with my code (python)?

friends = ['Sergey', 'Sonya', 'Dima', 'Alina', 'Egor']
index = print(friends[3])
print('Hi, ' + friends[index] + ', I'm Anfisa!' )

Add the code so that your program prints the phrase Hello, Alina, I'm Anfisa!
All that is required is to assign a value to the index variable so that Alina is selected from the list.
Do not change the line with the print() call.

this is the error: Traceback (most recent call last):
File "main.py", line 3, in
print('Hi, ' + friends[index] + ', I'm Anfisa!')
TypeError: list indices must be integers or slices , not str

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stefan, 2021-06-14
@Heonir

friends = ['Сергей', 'Соня', 'Дима', 'Алина', 'Егор']
index = 3
print('Привет,  ' + friends[index] + ',  я Анфиса!')

PS: I already replied to your last thread.

D
Denis Melnikov, 2021-06-14
@Mi11er

Well, it says that
the list index cannot be a string, but a number or a slice...

L
lil.fxrrx, 2021-06-14
@mxrdxfxrrx

Why do you enter print with the lines of the sheet as an index?
Of course, you give out the sheet friends, and also enter print(friends[3]) there, this is NoneType
friends = ['Sergey','Sonya','Dima','Alina','Egor ']
index = friends[3]
print('Hi, ' + index + ', I'm Anfisa!')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question