'How to hide player's input
I need the second user to not be able to see the first user's input, but when it tells the second user to input, the first user's input is right in front of them
score=[0,0]
print("Welcome to Rock Paper Scissors! The score starts as",score)
while True:
player1=input("Player 1's turn: ")
player2=input("Player 2's turn: ")
if (player1.lower() in ["rock","r","rick","rok","roc","rck"]):
if (player2.lower() in ["scissors","s","scissor"]):
score[0]=score[0]+1
print("Player 1 wins! The score is now",score)
if (player2.lower() in ["rock","r","rick","rok","roc","rck"]):
print("It's a tie! The score remains",score)
if (player2.lower() in ["paper","p","pap","piper"]):
score[1]=score[1]+1
print("Player 2 wins! The score is now",score)
if (player1.lower() in ["scissors","s","scissor"]):
if (player2.lower() in ["scissors","s","scissor"]):
score[0]=score[0]+0
print("It's a tie! The score remains",score)
if (player2.lower() in ["rock","r","rick","rok","roc","rck"]):
score[1]=score[1]+1
print("Player 2 wins! The score is now",score)
if (player2.lower() in ["paper","p","pap","piper"]):
score[0]=score[0]+1
print("Player 1 wins! The score is now",score)
if (player1.lower() in ["paper","p","pap","piper"]):
if (player2.lower() in ["scissors","s","scissor"]):
score[1]=score[1]+1
print("Player 2 wins! The score is now",score)
if (player2.lower() in ["rock","r","rick","rok","roc","rck"]):
score[0]=score[0]+1
print("Player 1 wins! The score is now",score)
if (player2.lower() in ["paper","p","pap","piper"]):
score[0]=score[0]+0
print("It's a tie! The score remains",score)
print("N E X T G A M E")
The output is:
Player 1's turn: r
Player 2's turn:
now, player 2 would just use paper and win the game, so i need to hide what player 1 inputted somehow (I'm using Python 3.6.1)
Solution 1:[1]
You can use getpass
here, to hide the player 1 's input and use input()
for the 2nd players input.
import getpass
player1 = getpass.getpass(prompt = "Player 1's turn:")
player2 = input("Player 2's turn")
Solution 2:[2]
I'm using Visual Studio Code Editor and when I run this code it clears the Terminal window where it executes without any issues.
import getpass, os
welcome = """
************************************************************
* *
* Welcome to the Rock, Paper, Scissors Game! *
* *
************************************************************
"""
print(welcome)
player1 = getpass.getpass('Rock, Paper, Scissors? ', stream = None)
os.system('cls')
Solution 3:[3]
You can use a bunch of print() lines to get past it.
player1move=input()
print("\n"*100)
player2move=input()
print("\n"*100)
#calculate who won
#print result
Solution 4:[4]
You can try erasing all the text from the console before asking Player 2 for their input.
import os
player1=input("Player 1's turn: ")
os.system('cls')
player2 = input("Player 2's turn: ")
Note that this solution would not work if using IDLE on Windows, since os.system()
does not interact with the IDLE shell (more details in this Stackoverflow answer).
For that, your best bet seems to be printing a lot of blank lines. You can create a function that does so, like this (from the above link):
def cls(): print "\n" * 100
Solution 5:[5]
Here is an example that worked for me without any issues:
import getpass
user = input('User: ')
password = getpass.getpass('Password: ')
password_length = len(password)
password_hide = ('*' * password_length)
print(f'{user}, your password {password_hide} is {password_length} digits long')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | user6903745 |
Solution 3 | Joshua Like |
Solution 4 | |
Solution 5 | Adrian Mole |