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"
))
Related
I try to increase the quality of this code, because Sonarqube doesn't like when there is too much if/elif.
I tried to use "swich case"; but this function is too young for my production environment.
Can i use dictionnary method with 2 conditions ?
This is my code :
if letter == "ABC" and orientation == "south" :
numero = "1"
elif letter == "ABC" and orientation == "east" :
numero = "2"
elif letter == "EDF" and orientation == "south" :
numero = "3"
elif letter == "EDF" and orientation == "east" :
numero = "4"
elif letter == "GHI" and orientation == "south" :
numero = "5"
elif letter == "GHI" and orientation == "north" :
numero = "6"
elif letter == "GHI" and orientation == "east" :
numero = "7"
You can create a mapping between letter, orientation and number and then fetch number from there:
values = {
("ABC", "south"): "1",
("ABC", "east"): "2",
("EDF", "south"): "3",
("EDF", "east"): "4",
("GHI", "south"): "5",
("GHI", "north"): "6",
("GHI", "east"): "7",
}
letter = "GHI"
orientation = "north"
number = values[(letter, orientation)]
If letter and orientation can have invalid values, make sure to enclose the number access in a try/except block:
try:
number = values[(letter, orientation)]
except KeyError as e:
print(f"No number defined for: {e}")
But when I input (0,1,0) ,(0,1,1),(1,0,0),(1,0,1). I either get none for the sum or none for the carryout. and I'm not sure what is wrong with the code and why I'm getting this issue. I think I have made most of the truth tables correctly based on their conditions I'm just not sure why I'm getting none as a sum when it should represent 1
***
def main():
a = input("Enter 'A' value(0-1): ")
b = input("Enter 'B' value(0-1): ")
c = input("Enter 'Cin' value(0-1): ")
def bitadder(a,b,c):
def xor_g(a,b):
if a != "1":
if b == "0":
if a == "0" and b == "0":
if a or b == "0":
return("0")
else:
return("1")
if a == "1":
if b != "0":
if a == "1" and b == "1":
if a or b == "1":
return("0")
else:
return("1")
if b == "1":
if a == "0":
if a == "0" and b == "1":
if a or b == "0":
return("1")
if b == "0":
if a == "1":
if a == "1" and b == "0":
if a or b == "1":
return("1")
#XOR logic gate
xor_g(a,b)
def and_g(a,b):
if a == "1" and b == "1":
return("1")
else:
return("0")
#AND logic gate
and_g(a,b)
def or_g(a,b):
if a == "0" or b == "0":
return("0")
else:
return("1")
#OR logic gate
or_g(a,b)
d = xor_g(a,b)
sum = xor_g(d,c)
g = and_g(d,c)
e = and_g(a,b)
out = xor_g(g,e)
print(sum,out)
bitadder(a,b,c)
#I think I have made most of the truth tables correctly based on their conditions I'm just not sure why I'm getting none as a sum when it should represent 1
***
main()
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.
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")
So I am creating a scroll-bar in appJar to show 10 numbers at a time (1-10, 11-20, etc.), and I am wondering what the best way is to go about this.
Should I create a function to hide the next 10 numbers until the scrollbar (which is a scale, though this might be the wrong method), and then they appear?
I am genuinely lost right now, as I've only started learning how to use appJar about 2 days ago for a project. Any ideas would be a great help. Yes, this is just the gui part, and the Python (3.6) has not been added yet, but will be.
from appJar import gui
def press(btn):
if btn == "1":
app.setLabel("answer", "1!")
elif btn == "2":
app.setLabel("answer", "2!")
elif btn == "3":
app.setLabel("answer", "3!")
elif btn == "4":
app.setLabel("answer", "4!")
elif btn == "5":
app.setLabel("answer", "5!")
elif btn == "6":
app.setLabel("answer", "6!")
elif btn == "7":
app.setLabel("answer", "7!")
elif btn == "8":
app.setLabel("answer", "8!")
elif btn == "9":
app.setLabel("answer", "9!")
elif btn == "10":
app.setLabel("answer", "10!")
app.startScrollPane("scroller")
elif btn == "11":
app.setLabel("answer", "11!")
elif btn == "12":
app.setLabel("answer", "12!")
app.stopScrollPane("scroller")
app=gui()
app.setFont(20)
app.addButtons(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"], press)
app.addScale("scroller")
app.setScaleChangeFunction("scroller", press)
app.setButton ("1", "1")
app.setButton ("2", "2")
app.setButton ("3", "3")
app.setButton ("4", "4")
app.setButton ("5", "5")
app.setButton ("6", "6")
app.setButton ("7", "7")
app.setButton ("8", "8")
app.setButton ("9", "9")
app.setButton ("10", "10")
app.setButton ("11", "11")
app.setButton ("12", "12")
app.addLabel ("answer", "Pick a Number!")
app.go()
I see two alternatives here:
Create a scrollPane, and utilise the scrollPane's built in scrollbars
Link the Scale widget to a function that updates the labels of the buttons
It really depends on how you want the GUI to look...
For option 1, try the following, instead of the scale:
app.startScrollPane("p1")
for i in range(1,101):
app.addButton(str(i), press, 0, i)
app.stopScrollPane()
For option 2, get rid of the list of setButton() calls, and replace them with a call to a new function: updateButtons().
Then, in that function, have something like:
def updateButtons(btn=None):
m = app.getScale("scroller") * 10
for i in range(1, 13):
app.setButton (str(i), str(m+i))
You'll also probably want to set the range on the scale: app.setScaleRange("scroller", 0, 9)
And, register the scale to call the updateButtons() function, instead of press
You'll also need to do the equivalent maths for the button presses, as they return their ID, not their value.