0
        if hint1 in ["yes", "y", "Y"]:
            secretword = ""
            for letter in Player1_secretword:
                secretword += letter
                print (secretword)
                break

I am currently a beginner building a guessing game and this is the code I have writtern in order for the program to give hints as the player requires. The first time the player needs a hint the first letter of the word is supposed to come up, the second time the player asks for a hint the second letter comes up, so on and so forth.

when I run the code this is the output:

enter cwould you like a hint? (Y/N): yes
c
ca 
cat 

I want the program to just give "c" the first time and, when the player asks for a hint the second time to give "ca" and so on, is there any possible way for me to achieve this?

  • Try to post a fully-functioning code for reproducibility. `break` exits the current loop and continues with the rest of the code. Where is the rest of the code that `break` goes to? – Mohamed Yasser Jul 31 '22 at 22:54
  • Consider incrementing a number each time a hint is requested, up to the length of their secrect word. You might then be able to use this number to "slice" the secret word string up to that number of characters. – David Foster Jul 31 '22 at 22:57
  • Does this answer your question? [Ways to slice a string?](https://stackoverflow.com/questions/1010961/ways-to-slice-a-string) – David Foster Jul 31 '22 at 22:58

2 Answers2

0

Why not create a variable named attempt, that you would increment every time the user asks for a hint?

You could then use this variable to control the length of the string to output.

Assuming Player1_secretword is the string containing the secret word, you could simply go for:

attempt=0
if hint1 in ["yes", "y", "Y"]:
    attempt+=1
    print(Player1_secretword[:attempt])
Sheldon
  • 4,084
  • 3
  • 20
  • 41
0

Instead of a for loop you can easily do this with 1 line of code:

def addHint(secret_word, hint):
    hint+= secret_word[len(hint)+1]

## and somewhere in your main
if input("do you want a hint") in ['Yes','y','Y']:
    addHint(secret, hint)
Farid Fakhry
  • 354
  • 1
  • 10