1

The code is a simple word guessing game, there is a designated secret word that the user will have to guess within a certain number of tries. Should the user think of asking for help he will be rewarded with a hint.

The problem is in line 10 and 11, my idea is that the user shouldn't have to guess an exact word like "help" to get the hint but could also guess a synonym of it for the same result. While line 10 works perfectly fine for the purpose of this program it takes a lot of space and if you want to include more works that produce the hint the code would become even more chaotic.

Line 11 on the other hand doesn't work at all, after wondering why for several minutes I looked into the or operator more closely and found that it doesn't compare the guess with the other words beyond the first one, but rather just checkes to see if the boolean value is True or not. Since every non empty string has the value True every single word would generate the hint.

Now the question is: Is there a workaround of comparing every single synonym with the guess of the user like in line 10, that is more compact. Or maybe a built in function that does a similar job of comparing multiple values with another value(in this case strings) that I didn't see?

def guessing_game():
    secret_word = "Schinken"
    guess = ""
    guess_count = 0
    guess_limit = 5
    print("Guess the secret word, you have " + str(guess_limit) + " tries")
    while guess != secret_word and guess_count != guess_limit:
        guess = input("Take a guess: ")
        guess_count += 1
        if guess.lower() == "hint" or guess.lower() == "tipp" or guess.lower() == "hinweis" or guess.lower() == "hilfe" or guess.lower() == "clue" or guess.lower() == "help" or guess.lower() == "advice":
        #if guess.lower() == "hint" or "help" or "hinweis" or "hilfe" or "tipp" or "clue" or "advice":
            print("Hint: German word for Ham")
        elif guess != secret_word and guess_count < guess_limit:
            print("Sorry that is wrong, you will have to try again")

    if guess == secret_word:
        print("Congratulations, you are correct! You Win!")
    else:
        print("Sorry, you ran out of guesses. You Lose!")
Jan Dreier
  • 23
  • 4

1 Answers1

2

You can put the options in a list and use in

if guess.lower() in ["hint", "tipp", "hinweis", "hilfe", "clue", "help", "advice"]:
Guy
  • 46,488
  • 10
  • 44
  • 88