Python not detecting when a variable is equal to another variable
I am making a program for my family for keeping ping pong scores. When the
score limit is reached, it should end the game and say who won. This works
for one player but not the other, and I can't figure out why. When P2's
score reaches 11, it says that P2 wins and restarts, but when P1's score
reaches 11, it just keeps on going. Please keep in mind that I am new to
this. Here is the code:
import sys
def defaults():
global p1sc
global p2sc
global p1adv
global p2adv
global p1name
global p2name
global sclimit
p1sc = 0
p2sc = 0
p1adv = 0
p2adv = 0
p1name = "Player 1"
p2name = "Player 2"
sclimit = 11
def checkwin():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
if p2sc < sclimit:
program()
if p1sc < sclimit:
program()
if p2sc <= sclimit:
print(p2name + " wins!")
defaults()
print("New game!")
program()
if p1sc <= sclimit:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif p1sc == sclimit - 1 and p2sc == sclimit - 1:
print("Sudden Death!")
suddendeath()
else:
print("DEBUG: CHECKWIN FAILED")
def displayscore():
global p1name
global p2name
global p1sc
global p2sc
print(p1name + ": " + str(p1sc))
print(p2name + ": " + str(p2sc))
def program():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
p1sc = p1sc + 1
displayscore()
checkwin()
elif cmdinput == "2":
p2sc = p2sc + 1
displayscore()
checkwin()
elif cmdinput == "q":
p1sc = p1sc - 1
displayscore()
checkwin()
elif cmdinput == "w":
p2sc = p2sc - 1
displayscore()
checkwin()
elif cmdinput == "name":
p1name = str(raw_input("Player 1 name: "))
print("Player 1 name has been set to " + p1name)
p2name = str(raw_input("Player 2 name: "))
print("Player 2 name has been set to " + p2name)
program()
elif cmdinput == "limit":
sclimit = raw_input("Score limit: ")
print("Score limit has been set to " + str(sclimit))
program()
elif cmdinput == "exit":
sys.exit()
else:
print("Command not recognised")
program()
def suddendeath():
global p1name
global p2name
global p1adv
global p2adv
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
if p2adv == 0:
p1adv = 1
elif p2adv == 1:
p2adv = 0
elif p1adv == 1:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif cmdinput == "2":
if p1adv == 0:
p2adv = 1
elif p1adv == 1:
p1adv = 0
elif p2adv == 1:
print(p2name + " wins!")
defaults()
print("New game!")
program()
defaults()
program()
No comments:
Post a Comment