尝试执行布尔 AND 时出现 TypeError
TypeError when trying to do a Boolean AND
我正在努力学习 python 但我在一些练习代码上卡住了。我想检查我放入名为 guesses
的列表中的三个项目中的两个是否在另一个名为 favorite
的列表中,同时检查我放入列表的第三个项目是否 guesses
不在另一个列表中 favorite
。
games = ['CS:GO', 'MK11', 'Black Ops 3', 'League of Legends', 'Osu!', 'Injustice 2', 'Dont Starve', 'Super Smash Brothers: Ultimate', 'God of War', 'Kingdom Hearts 3', 'Red Dead Redemption 2', 'Spider-Man', ]
favorite = ['God of War', 'CS:GO', 'Spider-Man']
guesses = ['', '', '']
print('I like ' + str(len(games) + 1) + ' games and here there are:' + str(games[:8]) + '\n' + str(games[9:]))
print('Can you guess whats are my favorite three games out of my list?')
guesses[0] = input()
print('Whats game number 2?')
guesses[1] = input()
print('Whats game number 3?')
guesses[2] = input()
# if all(x in favorite for x in guesses):
# print('Yes! Those are my three favorite games!')
if guesses[0] in favorite & guesses[1] in favorite & guesses[2] not in favorite:
print('Sorry, ' + str(guesses[0]) + ' & ' + str(guesses[1]) + ' are two of my favorite games but unfortunately ' + str(guesses[2]) + ' is not.')
我的问题是我认为我上面的 if 语句可以工作,请有人解释为什么我在下面收到这个 TypeError:
line 18, in <module>
if guesses[0] in favorite & guesses[1] in favorite & guesses[2] not in favorite:
TypeError: unsupported operand type(s) for &: 'list' and 'str'
我也知道 all
函数在这种情况下可以查看两个列表是否相等但所有项目是否相等但我想知道三个项目中的两个是否相等而第三个不相等t.
谢谢。
您在最后的 if 语句中使用了一个和号,而不是 and
。
if guesses[0] in favorite and guesses[1] in favorite and guesses[2] not in favorite:
print('Sorry, ' + str(guesses[0]) + ' & ' + str(guesses[1]) + ' are two of my favorite games but unfortunately ' + str(guesses[2]) + ' is not.')
一个符号表示 bitwise and which is done on binary types, and not boolean and,这就是您所需要的。
顺便说一句(如果这对您的程序很重要),值得注意的是,这只会检查您的 guesses
列表的一个排列(即如果 guesses[0]
不在 favourites
而不是 guesses[2]
?)
虽然可能不是最有效或最优雅的,但您可以使用 map
和 sum
:
# Turns each element of guesses into 1 if it's in favourites or 0 if not.
in_favourites = map(lambda x: 1 if x in favourites else 0, guesses)
# Sum the list of 1's and 0's
number_in_favourites = sum(in_favourites)
# Do your check (you could do number_in_favourites >= 2)
if number_in_favourites == 2:
print("Woopee!")
# Or more concisely:
if sum(map(lambda x: x in favourites, guesses)) == 2:
print("Woopee!")
(免责声明,我是纯粹在浏览器中编写这段代码,所以我没有测试过,但应该大致如此!)
我正在努力学习 python 但我在一些练习代码上卡住了。我想检查我放入名为 guesses
的列表中的三个项目中的两个是否在另一个名为 favorite
的列表中,同时检查我放入列表的第三个项目是否 guesses
不在另一个列表中 favorite
。
games = ['CS:GO', 'MK11', 'Black Ops 3', 'League of Legends', 'Osu!', 'Injustice 2', 'Dont Starve', 'Super Smash Brothers: Ultimate', 'God of War', 'Kingdom Hearts 3', 'Red Dead Redemption 2', 'Spider-Man', ]
favorite = ['God of War', 'CS:GO', 'Spider-Man']
guesses = ['', '', '']
print('I like ' + str(len(games) + 1) + ' games and here there are:' + str(games[:8]) + '\n' + str(games[9:]))
print('Can you guess whats are my favorite three games out of my list?')
guesses[0] = input()
print('Whats game number 2?')
guesses[1] = input()
print('Whats game number 3?')
guesses[2] = input()
# if all(x in favorite for x in guesses):
# print('Yes! Those are my three favorite games!')
if guesses[0] in favorite & guesses[1] in favorite & guesses[2] not in favorite:
print('Sorry, ' + str(guesses[0]) + ' & ' + str(guesses[1]) + ' are two of my favorite games but unfortunately ' + str(guesses[2]) + ' is not.')
我的问题是我认为我上面的 if 语句可以工作,请有人解释为什么我在下面收到这个 TypeError:
line 18, in <module>
if guesses[0] in favorite & guesses[1] in favorite & guesses[2] not in favorite:
TypeError: unsupported operand type(s) for &: 'list' and 'str'
我也知道 all
函数在这种情况下可以查看两个列表是否相等但所有项目是否相等但我想知道三个项目中的两个是否相等而第三个不相等t.
谢谢。
您在最后的 if 语句中使用了一个和号,而不是 and
。
if guesses[0] in favorite and guesses[1] in favorite and guesses[2] not in favorite:
print('Sorry, ' + str(guesses[0]) + ' & ' + str(guesses[1]) + ' are two of my favorite games but unfortunately ' + str(guesses[2]) + ' is not.')
一个符号表示 bitwise and which is done on binary types, and not boolean and,这就是您所需要的。
顺便说一句(如果这对您的程序很重要),值得注意的是,这只会检查您的 guesses
列表的一个排列(即如果 guesses[0]
不在 favourites
而不是 guesses[2]
?)
虽然可能不是最有效或最优雅的,但您可以使用 map
和 sum
:
# Turns each element of guesses into 1 if it's in favourites or 0 if not.
in_favourites = map(lambda x: 1 if x in favourites else 0, guesses)
# Sum the list of 1's and 0's
number_in_favourites = sum(in_favourites)
# Do your check (you could do number_in_favourites >= 2)
if number_in_favourites == 2:
print("Woopee!")
# Or more concisely:
if sum(map(lambda x: x in favourites, guesses)) == 2:
print("Woopee!")
(免责声明,我是纯粹在浏览器中编写这段代码,所以我没有测试过,但应该大致如此!)