2017-10-05 93 views
2

我还需要第二用户无法看到的第一个用户的输入,但是当它告诉第二用户输入,第一用户的输入是正确的,他们如何隐藏玩家的输入

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)

+2

你可以试试标准库中的'getpass'。 – cdarke

回答

0

你可以使用getpass h以隐藏玩家1的输入并使用input()作为第二位玩家输入。

import getpass 

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

当我使用getpass时,什么都没有发生。它只是空白的,没有“玩家1的回合”或任何东西出现 –

+0

'玩家1的回合:······ 玩家2的回合:随机'这是你使用它时得到的输出,请描述更多关于你的问题。 – bhansa

+0

https://imgur.com/XEfyUwT否“播放器1转:” –

0

在向玩家2请求输入之前,您可以尝试从控制台擦除所有文本。

import os 
player1=input("Player 1's turn: ") 
os.system('cls') 
player2 = input("Player 2's turn: ") 

注意,该解决方案将不若使用Windows IDLE,(在this Stackoverflow answer更多细节)工作,因为os.system()不与IDLE外壳进行交互。

为此,您最好的选择似乎是打印大量的空白行。你可以创建一个这样做的函数,像这样(从上面的链接):

def cls(): print "\n" * 100 
+0

使用此方法时没有清除提示 –

+0

它询问第一个用户,然后当他们进入输入时,它清除屏幕。这正是它对我的工作原理。你得到了什么? – Antimony

+0

https://imgur.com/rzgFN1F –

0

你可以使用一堆print()行来超过它。

player1move=input() 
print("\n"*100) 
player2move=input() 
print("\n"*100) 
#calculate who won 
#print result