I am new to Python (3rd day) and I was just trying to creat a basic Rock, Paper, Scissors. I am seeing a bug that I can't locate in the code and was hoping somebody could help. Here is the output below with the code following:
Welcome to Rock, Paper, Scissors!
Player 1 name?b
Player 2 name?s
3
2
1
GO!
Rock, Paper or Scissors?rock
b threw rock
s threw rock
Draw! Get Ready!.
3
2
1
GO!
Rock, Paper or Scissors?rock
b threw rock
s threw scissors
b win.
Rematch?no
Goodbye.
b win.
Rematch?
After entering "no" for rematch, it is printing "b win." also asking "Rematch?" again. Here is the code below:
import time
import random
picks=["rock","paper","scissors"]
answers=["yes","no"]
rock="rock"
paper="paper"
scissors="scissors"
yes="yes"
no="no"
invalid=""
#############################################Defining Functions#########################################################
def rename1():
global name1
while True:
if name1 is invalid:
name1 = input("Player 1 name?")
if name1 is not invalid:
break
def rename2():
global name2
while True:
if name2 is invalid:
name2 = input("Player 2 name?")
if name2 is not invalid:
break
def rematchinvalid():
global rematch
while True:
if rematch not in answers:
print("Invalid, try again..")
rematch = input("Rematch?")
if rematch in answers:
break
def Rethrow1():
global P1
while True:
if P1 not in picks:
print("Invalid, try again..")
P1 = input("Rock, Paper, or Scissors?")
if P1 in picks:
break
def start():
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("GO!")
def RPS():
global P1
global P2
global rematch
P1 = input("Rock, Paper or Scissors?")
P2 = random.choice(picks)
if P1 not in picks:
Rethrow1()
battle()
winner()
def battle():
print(name1," threw ",P1)
print(name2," threw ",P2)
def winner():
global rematch
if P1 == P2:
print("Draw! Get Ready!.")
start()
RPS()
if P1 == rock and P2 == scissors:
print(name1," win.")
if P1 == rock and P2 == paper:
print(name2," win.")
if P1 == scissors and P2 == rock:
print(name2," win.")
if P1 == scissors and P2 == paper:
print(name1," win.")
if P1 == paper and P2 == rock:
print(name1," win.")
if P1 == paper and P2 == scissors:
print(name2," win.")
rematch = input("Rematch?")
if rematch not in answers:
rematchinvalid()
replay()
def replay():
if rematch == yes:
start()
RPS()
if rematch == no:
print("Goodbye.")
################################################Game Start##############################################################
print("Welcome to Rock, Paper, Scissors!")
name1 = input("Player 1 name?")
if name1 is invalid:
rename1()
name2 = input("Player 2 name?")
if name2 is invalid:
rename2()
start()
RPS()
Also, if you have any recommendations on how to clean up the code, that would be appreciated!
Thanks
isfor equality.isis for identity...the two items are exactly the same object. Use==for equality.issometimes seems to work because the Python implementation may optimize common objects like empty strings and small numbers, but it will bite you. Try it withx=1000;y=123+877;x==y;x is y.