We assume curr_player to be 'X'.
Output should be 'O', and vice-versa
(is my code correct?)
#my code
def switchPlayer(curr_player):
if curr_player == "X" or curr_player == "x":
print("your Player 'O' ")
return curr_player
if curr_player == "O" or curr_player == "o":
print("your Player 'X' ")
return curr_player
switchPlayer("X")
You want to return the opposite of what it currently is. Something else I changed was the way you compare the strings. Instead of checking for uppercase and lowercase, you can convert the string to lowercase first, and you only need to compare it one time.
if (curr_player.lower() == 'x'):
return 'O'
if (curr_player.lower() == 'o'):
return 'X'
This code can also be simplified to this:
return 'O' if curr_player.lower() == 'x' else 'X'
def switchPlayer(curr_player):
if curr_player == "X" or curr_player == "x":
curr_player="O"
return curr_player
if curr_player == "O" or curr_player == "o":
curr_player="X"
return curr_player
inp=input("input the current player")
a=switchPlayer(inp)
print("returned player",a)
import difflib
import json
from difflib import SequenceMatcher
from difflib import get_close_matches
data=json.load(open("data.json"))
#word= input("enter a word:")
def tranlate(word):
word = word.lower()
if word in data:
return data[word]
elif len(get_close_matches(word,data.keys()))> 0 :
x=get_close_matches(word,data.keys())[0]
z = input("do u mean %s instead?Enter Y if yes or N if no" %x)
if z.lower() == "y" or "yes":
return data[x]
elif z.lower()== "n" or "no":
return "please enter correct word"
else:
return "we do not understand your entry"
else:
return "this word does not exixt,please double check it"
#ratio = SequenceMatcher(None, a, b).ratio()
word= input("enter a word:")
print(tranlate(word))
this below code are not running and it keeps executing
if z.lower() == "y" or "yes":
return data[x]
elif z.lower()== "n" or "no":
return "please enter correct word"
else:
return "we do not understand your entry"
Your if condition is incorrect :
It should be written as :
if z.lower() == "y" or z.lower()== "yes":
return data[x]
elif z.lower()== "n" or z.lower()== "no":
return "please enter correct word"
else:
return "we do not understand your entry"
Originally, you are doing : z.lower() == "y" or "yes"
this leads to : "y" or "yes", which always return true, and hence it will never go to the other conditions.
I am trying to rename the levels in my factor variable from
levels(data$VAL_NETEJA)
getting:
"0 = PÈSSIMA GESTIÓ" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10 = > >> > >EXCEL·LENT GESTIÓ" "NO HO SAP" "NO CONTESTA"
with this code
data %>%
filter(!is.na(VAL_NETEJA))%>%
transmute(VAL_NETEJA=case_when(
VAL_NETEJA == "10 = EXCEL·LENT GESTIÓ"~ "10",
VAL_NETEJA == "0 = PESSIMA GESTIÓ"~ "0",
TRUE ~ as.character(VAL_NETEJA)
))%>%
filter(!is.na(VAL_NETEJA))%>%
transmute(VAL_NETEJA=case_when(
VAL_NETEJA == "NO HO SAP"~ "NA",
VAL_NETEJA == "NO CONTESTA"~ "NA",
TRUE ~ as.character(VAL_NETEJA)
))%>%
and it works, except for "0 = PESSIMA GESTIÓ"~ "0". I was trying to try it with and without accents but it still won't change it. COuld I somehow resolve this issue with a function similar to contains or starts with for case_when?
So, at the and I put the 0 one as the base and used this code:
data$neteja_levels<-data %>%
transmute(VAL_NETEJA=case_when(
VAL_NETEJA == "10 = EXCEL·LENT GESTIÓ"~ "10",
VAL_NETEJA == "1"~"1",
VAL_NETEJA == "2"~"2",
VAL_NETEJA == "3"~"3",
VAL_NETEJA == "4"~"4",
VAL_NETEJA == "5"~"5",
VAL_NETEJA == "6"~ "6",
VAL_NETEJA == "7"~ "7",
VAL_NETEJA == "8"~ "8",
VAL_NETEJA == "9"~ "9",
VAL_NETEJA == "NO HO SAP"~"NA",
VAL_NETEJA == "NO CONTESTA"~ "NA",
VAL_NETEJA == "NA"~ "NA",
TRUE ~ "0"
))
def members_ID():
meme = False
membersID = str(input("enter members ID"))
while (meme) == False:
meme = True
if len(membersID) == 4:
if not (membersID)[0] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[1] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[2] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[3] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
else:
meme = False
print("members ID must be in the format 1111")
membersID = str(input("enter members ID"))
if meme == True:
print(membersID)
elif (meme) == False:
print("members ID must be in the format 1111")
if it is not four it works but if it is then the program will come to a halt and not work?
It may have something to do with the or operators as when I take them out and enter the required info with 4 digits then it works.
def members_ID():
meme = False
membersID = str(input("enter members ID"))
while (meme) == False:
meme = True
if len(membersID) == 4:
if not (membersID)[0] == "0":
meme = False
if not (membersID)[1] == "0":
meme = False
if not (membersID)[2] == "0":
meme = False
if not (membersID)[3] == "0":
meme = False
else:
meme = False
print("members ID must be in the format 1111")
membersID = str(input("enter members ID"))
if meme == True:
print(membersID)
elif (meme) == False:
print("members ID must be in the format 1111")
But not if the information entered is wrong. E.g., ssss will make the program stop working but 0000 will (if 0000 is the info required)!
You cannot use or in this way. The following:
not (membersID)[0] == "0" or "1"
is true whenever either not membersID[0] == "0" or the string "1" is non-empty, which it always is. Hence, the if statement is always true.
What you intended should be written as:
not (membersID[0] == "0" or membersID[0] == "1" or membersID[0] == "2") # etc. for 3..9
To avoid repeating yourself that much, you can make it this:
membersID[0] not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
The or-operator is between two statements or clauses, and returns the first part that is not None, False or equivalent (such as empty strings, lists etc.)
See this example:
a = 'a'
b = 'b'
print(a == 'b' or 'a')
# 'a'
print(a == 'a' or 'b')
# True
First, it prints out the value 'a' because a == 'b' is False, and 'a' is not empty, False or None.
Second, it prints out the value True because a == 'a' is True.
This is convenient if you want a value to be default if None:
x = some_variable or 5
Here, x will be 5 when some_variable is None, False (or empty).
So in your chain:
if not (membersID)[0] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
I would expect meme = False to run in every situation, because if you relabel it as:
a = not (membersID)[0] == "0"
b = "1"
c = "2"
#...
Then your if-statement would be
if a or b or c # or d ...
meme = False
Only the first clause, a will ever have the chance of not being True.
In other words, doing if something will be True if something is something that has value:
if "2":
print('This is a tautology')
Also, if not any of the clauses in a chain of ors is resolved as True, it will return the last clause:
x = None or False or None
print(x is None)
# True
print(type(x))
# NoneType
x = None or False
print(x)
# False
This is my completed awnser based on Thijs van dien's awnser.
(thanks)
def members_ID():
meme = False
membersID = input("enter members ID")
while (meme) == False:
meme = True
if len(membersID) == 4:
if membersID[0] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[1] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[2] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[3] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if meme == True:
print(membersID)
elif meme == False:
print("enter in the format 1111")
membersID = input("enter members ID")
else:
meme = False
print("members ID must be in the format 1111")
membersID = input("enter members ID")
membersID = str(input("enter members ID"))
input() already returns a string. In this line, you are trying to convert a string to a string when you enter a string. Therefore, it throws an error. Correct it to:
membersID = input("enter members ID")
This is my code:
import time,sys,random
wpm = 125
userName = ""
global mirror
mirror = False
global knife
knife = False
inventory = []
vial = False
trollHit = 0
playerHit = 0
axe = False
choice2 = False
kitchenAlready = False
def death():
sys.exit()
userInput = ""
def cantPerformAction(userInput):
if userInput == "e" or userInput == "u" or userInput == "w" or userInput == "s" or "t" in userInput:
slow_type("You can't perform that action.")
def checkDirection(userInput):
if any(char in userInput for char in 'nsewudt') == False:
return False
elif any(char in userInput for char in 'nsewudt') == True:
return True
#credit to 'bill gross' for ideas on the slow type
def slow_type(y): #slow typing to make it seem like the computer is typing
for x in y:
sys.stdout.write(x)
sys.stdout.flush()
time.sleep(random.random( )*10.0/wpm)
print (' ')
def clear(): #clear the line so that another line of text can appear below it
print ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
#CROSSROADS: All directions lead to death except right. Player cannot turn back.
def crossroads():
slow_type("\n\nCrossroads")
slow_type("\n\nA crossroads is in front of you. There is a passage to the west, east, and north.")
global choice2
global choice
choice = None
choice2 = None
youBetterChoose = 0
while choice == None and choice2 == None:
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput1 = userInput.lower().strip()
if userInput1 == "s":
clearingalready()
if userInput1 =="n":
slow_type("\n>You walk forward, determined... off a cliff. Tragic!")
death()
if userInput1 == "w":
slow_type("\nYou encounter a giant spider. Without a weapon, it easily tears you to shreds.")
death()
if userInput1 == "e":
fork()
if userInput1 == "d":
slow_type("You can't do down farther than the floor.")
if userInput1 == "u":
slow_type("You can't go that way.")
userInput = ""
def crossroadsalready():
slow_type("\n\nCrossroads")
global choice2
global choice
choice = None
choice2 = None
youBetterChoose = 0
while choice == None and choice2 == None:
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput1 = userInput.lower().strip()
if userInput1 == "s":
clearingalready()
if userInput1 =="n":
slow_type("\n>You walk forward, determined... off a cliff. Tragic!")
death()
if userInput1 == "w":
slow_type("\nYou encounter a giant spider. Without a weapon, it easily tears you to shreds.")
death()
if userInput1 == "e":
fork()
if userInput1 == "d":
slow_type("You can't do down farther than the floor.")
if userInput1 == "u":
slow_type("You can't go that way.")
userInput = ""
def fork():
choice2 = False
slow_type("\nFork")
slow_type("\nThere are another two ways to go. One goes north into a cavern, another leads down deeper into the earth.\n\n")
deeperOrFarther = ""
while choice2 == False:
userInput = input(">")
checkDirection(userInput)
if checkDirection(userInput) == False:
while checkDirection(userInput) == False:
userInput = input (">")
checkDirection(userInput)
deepLower = userInput.lower().strip()
if deepLower == "n":
slow_type("You walk forward, determined... off a cliff. Tragic!")
choice2 = True
death()
if deepLower == "d":
slow_type("You descend down into the darker regions of the cave.")
choice2 = True
if deepLower == "w":
crossroadsalready()
cantPerformAction(deepLower)
houseChoice()
wpm = 60
slow_type("Welcome to Zotan! \nn = north \ns = south \nw = west \ne = east \nu = up \nd = down \nt = take")
clear()
wpm = 120
def clearing():
slow_type("Clearing\n")
slow_type("You wake up in the middle of a clearing. There is a passage to the north.")
slow_type("\n\n")
clearingalready = True
choice = None
while choice == None:
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput = userInput.lower()
if userInput.strip() == "n":
choice = False
else:
slow_type("You can't go that way.")
userInput = ""
crossroads() #1
def clearingalready():
slow_type("Clearing\n")
slow_type("There is a passage to the north.")
slow_type("\n\n")
choice = None
while choice == None:
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput = userInput.lower()
if userInput.strip() == "n":
choice = False
else:
slow_type("You can't go that way.")
userInput = ""
crossroads() #1
def houseChoice():
slow_type("\n\n\n\nHouse Doorway\n\n")
choice = None
while choice == None:
userInput = input(">")
checkDirection(userInput)
if checkDirection(userInput) == False:
while checkDirection(userInput) == False:
userInput = input(">")
checkDirection(userInput)
userInput = userInput.lower().strip()
if userInput == "d" or userInput == "u" or userInput == "w" or userInput == "e":
slow_type("You can't go that way.")
if userInput == "n":
choice = False
if userInput == "s":
choice = False
if userInput == "s":
fork()
elif userInput == "n":
entryway()
def entryway():
slow_type("\nEntryway")
slow_type("\n\nThere is a kitchen to the west and a dark staircase. A back door hangs open in the north, revealing the entry into another dark corridor.")
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput = userInput.lower().strip()
if userInput == "w":
kitchen()
if userInput == "u":
attic()
if userInput == "n":
chasm()#4
if userInput == "s":
houseChoice()
def entrywayalready():
slow_type("\nEntryway")
userInput = input("\n>")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input("\n>")
checkDirection(userInput)
userInput = userInput.lower().strip()
if userInput == "w":
if kitchenAlready == False:
kitchen()
elif kitchenAlready == True:
kitchenalready()
if userInput == "u":
attic()
if userInput == "n":
chasm()#4
def kitchen():
kitchenAlready = True
slow_type("\n\nKitchen")
slow_type("It seems that the kitchen has been used recently. There is a wicked looking steak knife on the counter, and a mirror on the table.")
userInput = input("\n>")
userInput = userInput.lower().strip()
if userInput == "t knife":
if "knife" not in inventory:
slow_type("Taken.")
inventory.append("knife")
elif "knife" in inventory:
slow_type("You can't take that item.")
elif userInput == "t mirror":
if "mirror" not in inventory:
inventory.append("mirror")
elif "mirror" in inventory:
slow_type("You can't take that item.")
slow_type("You can't perform that action.")
elif userInput == "e":
entrywayalready()
def kitchenalready():
slow_type("\n\nKitchen")
userInput = input("\n>")
userInput = userInput.lower().strip()
if "knife" not in inventory:
inventory.append("knife")
elif "knife" in inventory:
slow_type("You can't take that item.")
if userInput == "t mirror":
if "mirror" not in inventory:
inventory.append("mirror")
elif "mirror" in inventory:
slow_type("You can't take that item.")
if userInput == "e":
entrywayalready()
def attic():
slow_type("\n\nAttic")
choice = None
while choice == None:
userInput = input(">")
if checkDirection(userInput) == False:
while checkDirection == False:
userInput = input(">")
checkDirection(userInput)
userInput = userInput.lower().strip()
if userInput == "n" or userInput == "s" or userInput == "e" or userInput == "w" or userInput == "u":
slow_type("You can't go that way")
if userInput == "t vial":
if "vial" not in inventory:
slow_type("Taken.")
inventory.append("vial")
elif "vial" in inventory:
slow_type("You can't take that item.")
if userInput == "d":
choice = False
entryway()#6
def chasm():
slow_type("\n\nChasm")
slow_type("\nThere is a chasm in front of you, with a rickety wooden bridge crossing the wide and endlessly deep ravine to the north, and a small passage to the west.")
userInput = input(">")
checkDirection(userInput)
if checkDirection(userInput) == False:
while checkDirection(userInput) == False:
userInput = input (">")
checkDirection(userInput)
chasmChoice = userInput.lower().strip()
if chasmChoice == "n":
slow_type("You cross the wooden bridge and it crumbles behind you. There is no turning back.")
cavern()
if chasmChoice == "w":
smallPassage()
if chasmChoice == "s":
entryway()#7
def smallPassage():
slow_type("\n\nSmall Passage")
slow_type("\nThere is a room to the west.")
userInput = input(">")
checkDirection(userInput)
if checkDirection(userInput) == False:
while checkDirection(userInput) == False:
userInput = input (">")
checkDirection(userInput)
userInputLower = userInput.lower().strip()
if userInputLower == "w":
trollRoom()
if userInputLower == "e":
chasm()
if userInputLower == "s" or userInputLower == "n" or userInputLower == "u" or userInputLower == "d" or userInputLower == "t":
slow_type("You can't perform that action.")#8
def trollRoom():
slow_type("\n\nTroll Room")
slow_type("\nThere is a troll in the room with a large blood stained axe.")
lowerr = "l"
while lowerr != "e":
userInput = input(">")
lowerr = userInput.lower().strip()
if lowerr == "attack troll":
if "knife" in inventory:
xRan = random.randint(0,3)
if xRan == "3":
slow_type("The troll parries your attack.")
if xRan == "2":
slow_type("You get a clear hit on the troll. It groans loudly, displaying a look of deep hatred on it's deformed face.")
trollHit = trollHit + 1
if trollHit == 2:
slow_type("The troll falls to the floor, dead. Its axe lays on the floor.")
axe = True
if userInput != "t axe" or userInput != "e":
slow_type("You can't perform that action.")
if xRan == 3:
if playerHit == 0:
slow_type("The troll knocks you back.")
if playerHit == 1:
slow_type("The troll lands a clean graze on your shoulder. You cry out in pain.")
if lowerr == "t axe":
if axe == True:
slow_type("Taken.")
inventory.append("axe")
elif axe != True:
slow_type("There is no axe there.")
if lowerr == "n" or lowerr == "s" or lowerr == "d" or lowerr == "u" or lowerr == "t" or lowerr == "w":
slow_type("You cannot perform that action.")#9
clearingAlready = False #Determines whether or not the player has already been to the crossroads.
clearing()
crossroads()
fork()
When I execute this particular part of the code It seems like there is no ouput from the computer, no Taken. or You can't perform that action. I can't seem to pinpoint the exact cause of the error or why it is misbehaving.
Console output:
House Doorway
>n
Entryway
>w
Kitchen
>t knife
output: nothing