Exit function in python
Question:
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN == "stop" or "exit":
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN == "stop" or "exit":
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
whenever I try to run this code the options to exit the program always displays. I only want this option to come available when the user enters exit or stop in the player one and player two input.
Answers:
You or
conditions are incorrect and just check that exit
is not null
which is always true.
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN == "stop" or player_oneN == "exit": # <--- should be this
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN == "stop" or player_twoN == "exit": # <--- should be this
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
The problem is that player_twoN == "stop" or "exit"
is not doing what you’re expecting. Its being interpreted as (player_twoN == "stop") or "exit"
. Since a non-empty string evaluates to True
, the whole statement evaluates to True
.
Use in
and check against a list of items when you want to check for multiple values in an if statement.
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN.lower() in ["stop", "exit"]: # replace with in list items
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN.lower() in ["stop", "exit"]: # replace with in list items
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
Since both your if statements are doing the same thing, you can combine them into one if statement.
if player_oneN.lower() in ["stop", "exit"] or player_twoN.lower() in ["stop", "exit"]:
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
You can also do something like this:
if any(i in ['stop','end'] for i in [player_oneN.lower(),player_twoN.lower()]):
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN == "stop" or "exit":
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN == "stop" or "exit":
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
whenever I try to run this code the options to exit the program always displays. I only want this option to come available when the user enters exit or stop in the player one and player two input.
You or
conditions are incorrect and just check that exit
is not null
which is always true.
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN == "stop" or player_oneN == "exit": # <--- should be this
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN == "stop" or player_twoN == "exit": # <--- should be this
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
The problem is that player_twoN == "stop" or "exit"
is not doing what you’re expecting. Its being interpreted as (player_twoN == "stop") or "exit"
. Since a non-empty string evaluates to True
, the whole statement evaluates to True
.
Use in
and check against a list of items when you want to check for multiple values in an if statement.
import random
import sys
player_oneN = input("Enter Name for Player One")
player_twoN = input("Enter Name for Player Two")
player_oneS = 0
player_twoS = 0
if player_oneN.lower() in ["stop", "exit"]: # replace with in list items
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
if player_twoN.lower() in ["stop", "exit"]: # replace with in list items
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
Since both your if statements are doing the same thing, you can combine them into one if statement.
if player_oneN.lower() in ["stop", "exit"] or player_twoN.lower() in ["stop", "exit"]:
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()
You can also do something like this:
if any(i in ['stop','end'] for i in [player_oneN.lower(),player_twoN.lower()]):
c = int(input("Press 1 to continue and 0 to quit :"))
if c == 0:
sys.exit()