PyProgrammerNoob
PyProgrammerNoob

Reputation: 45

Python hangman algorithm

So when i was programming a simple hangman game for fun, because of the beginner i am i still need help on things:

  1. I want my algorithm to test whether an input has only one character for example:

    while True:
      x=str(input())
      #code checking wheather it is only one character, if it is  
      character break and stop the loop, 
      otherwise repeat the input
    
  2. Let's say my secret word is 'billy' I would want to know how to how to check whether this one letter input has the same letter as any of those in the secret word. for e.g

    if x *code checking if the input has a same letter as the secret word*:
      #carry on program
    

If you can help me with either of these two problems you'd be a lifesaver!

Upvotes: 2

Views: 115

Answers (1)

K-D-G
K-D-G

Reputation: 287

You could use if in when checking. So you get the input and then you would use code like the following:

if x in secret_word:
  #carry on with the program

To check for it having one character use len. So

if len(x)==1:
  #carry on

Upvotes: 5

Related Questions