Вы можете еще больше упростить свой код, используя itertools.permutations()
.
Пример:
import itertools
names = ['alex','tom','james']
unique = set()
permutations = set(itertools.permutations(names, 2))
for pair in permutations:
if pair not in unique and pair[::-1] not in unique:
unique.add(pair)
relationships = []
for pair in unique:
strength = input(f'what is the relationship between {pair[0]} and {pair[1]}?')
relationships.append((pair,strength))
print(relationships)
Вывод на консоль:
what is the relationship between alex and james?12
what is the relationship between alex and tom?5
what is the relationship between james and tom?6
[(('alex', 'james'), '12'), (('alex', 'tom'), '5'), (('james', 'tom'), '6')]
Однако даже вашкод, кажется, работает нормально в ноутбуке Jupyter:
Вы можете попробовать перезапустить ядро Python, т.е. в меню перейдите к Kernal -> Restart иRun all.
Смежный вопрос:
Как дать стандартный ввод ячейки юпитера в python?