0

I'm trying to write a hang-man game in python 3. I choose a player to enter the secret word and check to ensure there are no numbers, special characters, etc.. What i want to be able to do is give the other player the chance to pick the word if the current player tries to break these rules. Then i want that to keep alternating until someone enters a word that meets the criteria. What I have now...

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()
while True:
    if word_to_guess.isalpha():
        break
    word_to_guess = input("OK, " + Second_player + ", since " + First_player + " can't follow the rules, you try it. Again, no numbers, spaces or special characters: ").lower()

So if the first player gets it, we move on, if the first player fails and the second player gets it, we move on, but if they both fail, the code as written will just keep giving player 2 the choice of word. I essentially just want to alternate the position of the "First_player" and "Second_player" variable locations in that last statement, with each repetition. Any ideas?

2 Answers 2

1

It think the First_player and Second_player hard coded in your 5th line might be throwing you off a bit.

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()

turn_player = First_player
last_player = Second_player

while True:
    if word_to_guess.isalpha():
        break
    turn_player, last_player = last_player, turn_player
    word_to_guess = input("OK, " + turn_player + ", since " + last_player + " can't follow the rules, you try it. Again, no numbers, spaces or special characters: ").lower()
Sign up to request clarification or add additional context in comments.

Comments

1

I think that you can use flag variable, which will show whose turn is it

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()
is_first_player_turn = False
while True:
 if is_first_player_turn:
  #First player's turn
 else:
  #Second player's turn
 is_first_player_turn = not is_first_player_turn 

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.