in Education by
I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : mylist.append("c") if x or y or z == 1 : mylist.append("d") if x or y or z == 2 : mylist.append("e") if x or y or z == 3 : mylist.append("f") which would return a list of ["c", "d", "f"] Is something like this possible? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
The correct and fast way to test multiple variables against one value is by using set with separate if-statements so that Python will read each statement whether the former were True or False. Such as: x=0 y=1 z=3 mylist=[] if 0 in {x, y, z}: mylist.append("c") if 1 in {x, y, z}: mylist.append("d") if 2 in {x, y, z}: mylist.append("e") if 3 in {x, y, z}: mylist.append("f") print(mylist) This code will also work, but if you are comfortable using dictionaries you can clean this up by making an initial dictionary mapping the numbers to the letters you want, then just by using a for-loop: x=0 y=1 z=3 mylist=[] num_to_letters = {0: "c", 1: "d", 2: "e", 3: "f"} for number in num_to_letters: if number in {x, y, z}: mylist.append(num_to_letters[number]) print(mylist)

Related questions

0 votes
    Sometimes I break long conditions in ifs onto several lines. The most obvious way to do this is: if (cond1 ... an alternative way? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have a code: ab = 'off' if b.page == 'blog': ab = 'on' print ab Is there any other way to make it shorter in Python? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Myself and a colleague have a dispute about which of the following is more elegant. I won't say ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I'm working on a mini text role playing game where you must survive dinner with the king. I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 8, 2022 in Education by JackTerrance
0 votes
    I have a python variable how can I see that it's unsigned 32 bit or signed 16 bit, etc? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How does one write a unittest that fails only if a function doesn't throw an expected exception? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I've recently seen quite a bit of the following pattern in Python: class Foo: pass class Bar: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    Jupyter notebooks (Python) return the value of the last variable in a cell in a pretty printed format. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I started learning python a few months back, so i don't know alot of terms still. This program ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    In a function, How to create and use a global variable? How can I use it in other functions? Is it compulsory ... it in a function? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
...