0

I am creating a hangman game for my GCSE Computing Project. I have come across an error that I cannot seem to rectify.

This is all of the code:

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.")

When I run the program, I get this error

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

I am not sure how to fix this error, can someone please help me?

Also, if you're able to, can you give me some advice on what to improve any section of the whole code.

*This is my first question, forgive me if I've been a bit extensive"

5
  • Why would you ever do for i in range(1)? Commented Apr 7, 2014 at 13:07
  • Show us the full stack trace. Commented Apr 7, 2014 at 13:07
  • @user2357112 So one letter is accepted for each variable Commented Apr 7, 2014 at 13:27
  • Whatever you think the loop is doing, it's probably not doing that. The loop is exactly equivalent to not having a loop at all and just setting i=0. Commented Apr 7, 2014 at 13:38
  • @user2357112 I'm not sure what to do, that was the only way I got my hangman game to give a response for each individual letter inputted. Commented Apr 7, 2014 at 13:58

1 Answer 1

0

You should do it as follows instead:

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

This will print 'Well done, you got it.' only if all the letters are in word. It makes use of the all built-in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.