2017-10-05 52 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