Home > Software engineering >  Hiding user input in pyhton
Hiding user input in pyhton

Time:07-26

I want hide take_stick = int(input()) so that it doesn't print the user input as it is supposed to get printed in a different place, but I don't know how to do it. I need the output to look like this:

Game of sticks
Player 1 enter how many sticks to remove: 2
There are 19 sticks left
Player 2 enter how many sticks to remove: 4
Must remove between 1-3 sticks! 

... and so on, but now the output gives the input numbers between each line (for obvious reasons). This is the code so far:

def main():
    print("Game of sticks")
    player_turn = 1
    starting_sticks = 21
    sticks_left = starting_sticks

    while True:
        if starting_sticks == 21:
            if sticks_left < 1:
                print("There are", sticks_left, "sticks left")
            take_stick = int(input())

            if take_stick < 1 or take_stick > 3:
                print("Must remove between 1-3 sticks!")

            elif take_stick >= 1 and take_stick <= 3:
                sticks_left -= take_stick
            if sticks_left == 21:
                print("Player", player_turn, "enter how many sticks to remove:", take_stick)
                player_turn  = 1
                if player_turn == 3:
                    player_turn -= 2
            elif sticks_left != 1:
                print("Player", player_turn, "enter how many sticks to remove:", take_stick)
                player_turn  = 1
                if player_turn == 3:
                    player_turn -= 2
                print("There are", sticks_left, "sticks left")

            if sticks_left <= 0:
                print()
                print("Player", (player_turn), "lost the game!")
                break
main()

CodePudding user response:

As already suggested you could use getpass with the default prompt set to empty string. Moreover, you could lift the cursor one line up to not have an empty line (if possible in your terminal).

Example:

from getpass import getpass
take_stick = int(getpass(prompt=''))

CodePudding user response:

I don't think you need to hide any input; you just need to restructure your loop to match the flow of the game. Something like

def main():
    print("Game of sticks")
    player_turn = 1
    sticks_left = 21

    while sticks_left > 0:
        print("There are", sticks_left, "sticks left")
        while True:
            take_stick = int(input(f"Player {player_turn} enter how many sticks: ")    
            if 1 <= take_stick <= 3:
                break
            print(f"Must remove between 1-3 sticks!")

        sticks_left -= take_stick
        player_turn = 2 if player_turn == 1 else 1

    print(f"Player {player_turn} lost the game!")

main()

The whole game is just:

  1. State how many sticks are left
  2. Let the current player take some sticks
  3. Update whose turn it is
  4. Repeat until all the sticks are gone
  5. Report the currently player as the loser (since there are no sticks for them to take)

CodePudding user response:

If I understood correctly, I think this is what you want to do :

def main():
    print("Game of sticks")
    player_turn = 1
    starting_sticks = 21
    sticks_left = starting_sticks

    while True:
        if starting_sticks == 21:
            if sticks_left < 1:
                print("There are", sticks_left, "sticks left")
            take_stick = int(input(f"Player {player_turn} enter how many sticks to remove: "))

            if take_stick < 1 or take_stick > 3:
                print("Must remove between 1-3 sticks!")
                continue

            elif take_stick >= 1 and take_stick <= 3:
                sticks_left -= take_stick
            if sticks_left == 21:
                player_turn  = 1
                if player_turn == 3:
                    player_turn -= 2
            elif sticks_left != 1:
                player_turn  = 1
                if player_turn == 3:
                    player_turn -= 2
                print("There are", sticks_left, "sticks left")

            if sticks_left <= 0:
                print()
                print("Player", (player_turn), "lost the game!")
                break
main()

The input function takes a parameter, prompt, which is a string representing a message before the input. This allows the user to input its number at the end of the line, and not in between lines. You therefore don't need the prints with said string, which I removed. I also added a continue statement, so that if a player takes too many sticks, it stays his turn.

  • Related