Skip to main content
 首页 » 编程设计

python之如何隐藏玩家的输入

2025年02月15日7langtianya

我需要第二个用户看不到第一个用户的输入,但是当它告诉第二个用户输入时,第一个用户的输入就在他们面前

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") 

输出是:

Player 1's turn: r 
Player 2's turn:  

现在,玩家 2 只需使用纸来赢得游戏,所以我需要隐藏玩家 1 输入的内容(我使用的是 Python 3.6.1)

请您参考如下方法:

您可以使用 getpass在这里,要隐藏玩家 1 的输入并使用 input() 作为第二个玩家的输入。

import getpass 
 
player1 = getpass.getpass(prompt = "Player 1's turn:") 
player2 = input("Player 2's turn")