Home > Enterprise >  I Want To Print The Next Item In A List After A Condition Is Met
I Want To Print The Next Item In A List After A Condition Is Met

Time:07-05

I'm new to python and I'm iterating on a small starter project from a tutorial. It's a little word guessing game. I have a master list of hints for specific secret words and I want to print a hint and after a wrong guess print the next hint in line.

I'm wanting to accomplish this without using a bunch of if statements because I think there's gotta be a more efficient shorthand way of doing it.

    import random

secret_word_list = ["Duck"]
duck_hint_list = ["Hint: It's a bird", "Hint: it swims", "Hint: It loves to be in a row"]
secretWord = random.choice(secret_word_list)
guess = ""
guess_limit = 3
guess_attempts = 0
game_end = False

while guess != secretWord and guess_attempts != guess_limit:
    if secretWord == "Duck":
        if guess_attempts != guess_limit:
            print(random.choice(duck_hint_list))
    guess = (input("Guess the secret word: "))
    guess_attempts  = 1

if guess_attempts == guess_limit:
    print("YOU LOSE IDIOT")
else:
    print("You Win!")

As you can see I tried using random.choice but the obvious downside to that is it will print the same hint twice.

CodePudding user response:

Start with an index at 0, and use it to print a hint when needed. When you do print a hint, increment the index by 1.

If you want to cycle through the hints, potentially repeating them, use: index % len(duck_hint_list) as your index.

If you wish the hints to come up in random order, first use random.shuffle on duck_hint_list.

CodePudding user response:

# import random

secret_word_list = "Duck"
duck_hint_list = ["Hint: It's a bird", "Hint: it swims", "Hint: It loves to be in a row"]
# secretWord = random.choice(secret_word_list)
guess = ""
guess_limit = 3
guess_attempts = 0
game_end = False
i=0 #here i intialized zero
while guess != secret_word_list and guess_attempts != guess_limit:
    # if secretWord == "Duck":
        if guess_attempts != guess_limit:
           # print(random.choice(duck_hint_list))
           print(duck_hint_list[i])#indexing
        guess = (input("Guess the secret word: "))
        guess_attempts  = 1
        i =1#incrementing

if guess_attempts == guess_limit:
    print("YOU LOSE IDIOT")
else:
    print("You Win!")

there are some code which i commented that are not in use. if you want new hint each time they you should use indexing which i did above and i also commented the part which i updated.

CodePudding user response:

in the original code, some variables and statements were not in use so I didn't include them in the revised code. I solved the problem of printing a new item in the list by removing the already shown hint, that way it will prevent a repetition of the hint. Also, it is always easier to make your condition for a while loop one clear-cut condition so you can exit the loop easier. I hope this helps you.

import random

secret_word = "Duck"
duck_hint_list = ["Hint: It's a bird", "Hint: it swims", "Hint: It loves to be in a row"]
guess_limit = 3
guess_attempts = 0
game_end = True #while loop condition 

while game_end:
    hint = random.choice(duck_hint_list) #picking a random hint from hint list
    print(hint)
    duck_hint_list.remove(hint) #removing of already shown hint
    guess = (input("Guess the secret word: "))
    guess_attempts  = 1
    
    if guess_attempts == guess_limit:
        print("YOU LOSE IDIOT")
        game_end = False #while loop condition change (breaking the loop)
    elif guess.lower() == secret_word.lower() :
        print("You Win!")
        game_end = False #while loop condition change (breaking the loop)
  • Related