-1

i am creating a rock, paper, scissors game for 2 PLAYERS. However, when the first player enters rock, paper or scissors, i want their input to vanish so then the second player can then enter theirs. Becuase i dont want them seeing each others entries. Is this possible in Python 3.5.0 ? any help would be much appreciated! thanks! i do not want to clear the whole screen, just ONE line!

  • I would probably use `sys.stdout.write` if the input is allowed to be visible as it is typed but should disappear after the user presses enter. If you want it to be replaced by `*` *while* the user types - like in a real password prompt - there are differences between linux and windows that need to be considered (I haven't found a simple way of evaluating input without pressing enter in linux - only by importing pygame which seems excessive. On windows, it's easier. Getpass might be the simplest cross-platform solution in this case). What are your exact requirements? – jDo Mar 22 '16 at 11:56

2 Answers2

1

You could use getpass, which is normally used for entering passwords, so the user's input is not printed to the terminal.

Here's an example of running this in the interpreter, the text 'Rock' that I entered at the prompt was not shown, but is available in the 'choice' variable.

>>> import getpass
>>> choice = getpass.getpass("Rock/Paper/Scissors:")
Rock/Paper/Scissors:
>>> choice
'Rock'
>>>

You'd want to put this in a loop which checked that the user had entered one of the valid options, so you'd have something like:

import getpass

valid_options = ["Rock", "Paper", "Scissors"]
valid_choice = False
while (not valid_choice):
    choice = getpass.getpass("Rock/Paper/Scissors:")
    if (choice in valid_options):
        valid_choice = True
    else:
        print("Invalid option. Please try again")
emmagordon
  • 1,222
  • 8
  • 17
  • How would i go about adding that to my code, couyld you give an example, say with the input, rock, paper or scissors and then your funtion? thanks! – John Smith Mar 22 '16 at 11:37
  • I've added an example of getting the user's option and checking it is one of the valid options. – emmagordon Mar 22 '16 at 11:46
  • I entered that, but i get this error.: – John Smith Mar 22 '16 at 11:47
  • Warning (from warnings module): File "C:\Program Files (x86)\Python 3.5\lib\getpass.py", line 101 return fallback_getpass(prompt, stream) GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed. Rock/Paper/Scissors: – John Smith Mar 22 '16 at 11:48
  • See this [answer](http://stackoverflow.com/questions/21264701/getpass-getpass-error-without-other-code) - are you using IDLE? Running in cmd.exe or Windows PowerShell is recommended to avoid this problem (code above runs fine on my linux machine). – emmagordon Mar 22 '16 at 11:56
0

You can use an escape sequence:

valid = ("rock", "paper", "scissors")
choice = input("What is your choice? ").lower()
while choice not in valid:
    print("\033[1A\033[K", end="")
    choice = input("Invalid choice.  Try again: ").lower()
print("\033[1A\033[K", end="")
zondo
  • 19,901
  • 8
  • 44
  • 83