Reputation: 84
So...I just want this. I'm trying to make a game 'Trusting Game' in Python. This is a game that you can betray your partner or not. here's the code I was making:
p1_point = 0
p2_point = 0
x = int(input("How many levels? "))
while x != 0:
p1 = input("Player 1, pick yes to agree or no to betray. ") #I want to change this.
p2 = input("Player 2, pick yes to agree or no to betray. ") #I want to change this.
if p1 == "yes" and p2 == "yes":
p1_point += 5
p1_point += 5
elif p1 == "yes" and p2 == "no":
p1_point -= 2
p2_point += 10
elif p1 == "no" and p2 == "yes":
p1_point += 10
p2_point -= 2
elif p1 == "no" and p2 == "no":
p1_point -= 5
p2_point -= 5
print("Player 1 said {} and Player 2 said {}.".format(p1, p2))
print("Player 1's score is now {} and Player 2's score is now {}.".format(p1_point, p2_point))
x -= 1
if p1_point > p2_point:
print("Player 1 wins!")
elif p1_point < p2_point:
print("Player 2 wins!")
elif p1_point == p2_point:
print("It's a tie!")
but I want to make the input like this:
****
so they won't cheat.
I saw someone saying to use the getpass module, I used it like this.
p1 = getpass.getpass(prompt = "Player 1, pick yes to agree or no to betray. ", stream = None)
p2 = getpass.getpass(prompt = "Player 2, pick yes to agree or no to betray. ", stream = None)
but there was a getpassWarning.
(from warnings module):
File "C:\Program Files (x86)\Microsoft Visual
Studio\Shared\Python36_64\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Player 1, pick yes to agree or no to betray.
How can I fix this? And how do I use the getpass?
Upvotes: 0
Views: 233
Reputation: 1413
You can use getpass
import getpass
pswd = getpass.getpass('Password:')
This should do the job. If you get a getPassWarning, you can try this
import sys
import msvcrt
passwor = ''
while True:
x = msvcrt.getch()
if x == '\r':
break
sys.stdout.write('*')
passwor +=x
print '\n'+passwor
Upvotes: 1