TypeError:&#39;在<string>&#39;要求字符串作为左操作数,而不是列表</string>

时间:2014-04-07 13:05:49

标签: python python-3.x

我正在为我的GCSE计算项目创建一个刽子手游戏。我遇到了一个我似乎无法纠正的错误。

这是所有代码:

import random

#Creates a tuple with words in
WORDS = ("church","smelly","chicks")


#This picks a word randomly
word = random.choice(WORDS)
print("The word is", len(word), "letters long.")


#This is the game
for i in range(1):
letter1 = input ("Guess a letter:")

if letter1 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

for i in range(1):
letter2 = input ("Guess a letter:")

if letter2 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

for i in range(1):
letter3 = input ("Guess a letter:")

if letter3 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

for i in range(1):
 letter4 = input ("Guess a letter:")

if letter4 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

for i in range(1):
letter5 = input ("Guess a letter:")

if letter5 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

for i in range(1):
letter6 = input ("Guess a letter:")

if letter6 in word:
 print("Correct. This letter is in the word")
else:
 print("Incorrect. This letter is not in the word.")

all_letters = [letter1, letter2, letter3, letter4, letter5, letter6]

# Conditions for winning/losing
if all_letters in word:
 print("Well done, you got it.")
else:
 print("Sorry, You are the loser.")

当我运行程序时,我收到此错误

TypeError: 'in <string>' requires string as left operand, not list

我不确定如何修复此错误,有人可以帮助我吗?

此外,如果您能够,您能否就如何改进整个代码的任何部分给我一些建议。

*这是我的第一个问题,请原谅我,如果我有点广泛&#34;

1 个答案:

答案 0 :(得分:0)

你应该这样做:

if all(i in word for i in all_letters):
    print("Well done, you got it.")
else:
    print("Sorry, You are the loser.")

仅当所有字母都在单词中时,才会打印'Well done, you got it.'。它使用内置的all