저는 제 자신에게 파이썬을 가르치고 있습니다. 나의 작은 시작 프로젝트 중 하나는 Rock, Paper, Scissors 게임입니다.getpass를 사용하여 터미널에서 사용자 입력을 숨 깁니다.
코드가 정상적으로 실행됩니다. 그래도 추가 기능을 추가하고 싶습니다. 사용자가 Rock, Paper 또는 Scissor를 입력 할 때마다 입력은 터미널에 남아 있습니다. 물론 두 번째 플레이어에게는 불공정 한 상황이 발생합니다.
이 문제를 피하기 위해 getpass 함수를 사용하고 있습니다. 불행하게도, 내 코드에서 P1inp와 P2inp로 getpass를 사용한 후에도 입력은 여전히 터미널에 남아 있습니다. 누구든지 더 나은 해결책을 지적하거나 올바른 방향으로 나를 밀어 줄 수 있습니까?
import sys
import getpass
rules = "Rules:Rock beats Scissors, Scissors beats Paper, and Paper beats Rock"
print(rules)
print("Would you like to play?")
decision = input("Yes or No?")
P1 = str(input("What's the name of Player 1?"))
P2 = str(input("What's the name of Player 2?"))
P1inp = getpass.getpass(input("%s, Rock, Paper or Scissors?"%P1))
P2inp = getpass.getpass(input("%s, Rock, Paper or Scissors?"%P2))
def play(decision):
if decision == "Yes":
compare(P1inp,P2inp)
else:
print("See you next time!")
def compare(P1inp,P2inp):
if P1inp == P2inp:
print("It's a tie!")
elif P1inp == "Rock" and P2inp == "Scissors":
print("%s wins!!"%P1)
elif P1inp == "Rock" and P2inp == "Paper":
print("%s wins!!"%P2)
elif P1inp == "Paper" and P2inp == "Scissors":
print("%s wins!!"%P2)
elif P1inp == "Paper" and P2inp == "Rock":
print("%s wins!!"%P1)
elif P1inp == "Scissors" and P2inp == "Rock":
print("%s wins!!"%P2)
elif P1inp == "Scissors" and P2inp == "Paper":
print("%s wins!!"%P1)
else:
return("Invalid input")
sys.exit()
print(compare(P1inp,P2inp))
print ("Would you like to play again?")
result = input("Yes or No?")
while result == "Yes":
samePlayers = input("Are P1 and P2 still the same?")
if samePlayers == "Yes":
P1inp = input("%s, Rock, Paper or Scissors?"%P1)
P2inp = input("%s, Rock, Paper or Scissors?"%P2)
play(result)
print(compare(P1inp,P2inp))
print ("Would you like to play again?")
result = input("Yes or No?")
else:
P1 = str(input("What's the name of Player 1?"))
P2 = str(input("What's the name of Player 2?"))
P1inp = input("%s, Rock, Paper or Scissors?"%P1)
P2inp = input("%s, Rock, Paper or Scissors?"%P2)
play(result)
print(compare(P1inp,P2inp))
print ("Would you like to play again?")
result = input("Yes or No?")
else:
print("Thanks for playing!")
파이썬 3에서는'input()'은 항상 문자열을 반환하므로 (숫자를 쓰더라도)'str()'-'P1 = input (...)'이 필요하지 않습니다. 누군가가'예'또는'예'또는'예'또는 이벤트'예스'를 입력 할 수 있기 때문에 BTW :'input(). lower()'를 사용하고 결과를 소문자 텍스트'예'와 비교하십시오. – furas