I want it to give me error when is not 0 or 1 [on hold]
up vote
-5
down vote
favorite
#!/usr/bin/python
def open_doors(state):
(ld, rd, li, ri, lo, ro, cl, ml, gs) = state
if not(ld == 0 or rd == 0 or li == 0 or ri == 0 and lo == 0 or ro == 0 or cl == 0 or ml == 0):
raise ValueError("Error: A state is not specified or invalid entry")
if not(ld == 1 or rd == 1 or li == 1 or ri == 1 and lo == 1 or ro == 1 or cl == 1 or ml == 1):
raise ValueError("Error: A state is not specified or invalid entry")
leftDoor = (ld or li or lo)
rightDoor = (rd or ri or ro)
if (li and cl):
leftDoor = 0
if (ri and cl):
rightDoor = 0
if leftDoor and rightDoor and not ml and gs == 'P':
print("left door and right door open")
elif leftDoor and not ml and gs == 'P':
print("open left door")
elif rightDoor and not ml and gs == 'P':
print("open right door")
else:
print("both doors stay closed")
open_doors([0,0,1,6,0,0,1,0,'2'])
Raises ValueError if not every state is specified,
the switches and locks are not 0 or 1 or the gs is
not one of ('P' 'R', 'N', '2', '3' 'D')
python python-3.x
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as unclear what you're asking by Yu Hao, usr2564301, tripleee, ewolden, Madhur Bhaiya 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-5
down vote
favorite
#!/usr/bin/python
def open_doors(state):
(ld, rd, li, ri, lo, ro, cl, ml, gs) = state
if not(ld == 0 or rd == 0 or li == 0 or ri == 0 and lo == 0 or ro == 0 or cl == 0 or ml == 0):
raise ValueError("Error: A state is not specified or invalid entry")
if not(ld == 1 or rd == 1 or li == 1 or ri == 1 and lo == 1 or ro == 1 or cl == 1 or ml == 1):
raise ValueError("Error: A state is not specified or invalid entry")
leftDoor = (ld or li or lo)
rightDoor = (rd or ri or ro)
if (li and cl):
leftDoor = 0
if (ri and cl):
rightDoor = 0
if leftDoor and rightDoor and not ml and gs == 'P':
print("left door and right door open")
elif leftDoor and not ml and gs == 'P':
print("open left door")
elif rightDoor and not ml and gs == 'P':
print("open right door")
else:
print("both doors stay closed")
open_doors([0,0,1,6,0,0,1,0,'2'])
Raises ValueError if not every state is specified,
the switches and locks are not 0 or 1 or the gs is
not one of ('P' 'R', 'N', '2', '3' 'D')
python python-3.x
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as unclear what you're asking by Yu Hao, usr2564301, tripleee, ewolden, Madhur Bhaiya 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
So you just want to make your code more concise? Also, is the singleandstatement in your if condition a typo?
– DocDriven
2 days ago
1
look upany()andall()
– Chris_Rands
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago
add a comment |
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
#!/usr/bin/python
def open_doors(state):
(ld, rd, li, ri, lo, ro, cl, ml, gs) = state
if not(ld == 0 or rd == 0 or li == 0 or ri == 0 and lo == 0 or ro == 0 or cl == 0 or ml == 0):
raise ValueError("Error: A state is not specified or invalid entry")
if not(ld == 1 or rd == 1 or li == 1 or ri == 1 and lo == 1 or ro == 1 or cl == 1 or ml == 1):
raise ValueError("Error: A state is not specified or invalid entry")
leftDoor = (ld or li or lo)
rightDoor = (rd or ri or ro)
if (li and cl):
leftDoor = 0
if (ri and cl):
rightDoor = 0
if leftDoor and rightDoor and not ml and gs == 'P':
print("left door and right door open")
elif leftDoor and not ml and gs == 'P':
print("open left door")
elif rightDoor and not ml and gs == 'P':
print("open right door")
else:
print("both doors stay closed")
open_doors([0,0,1,6,0,0,1,0,'2'])
Raises ValueError if not every state is specified,
the switches and locks are not 0 or 1 or the gs is
not one of ('P' 'R', 'N', '2', '3' 'D')
python python-3.x
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
#!/usr/bin/python
def open_doors(state):
(ld, rd, li, ri, lo, ro, cl, ml, gs) = state
if not(ld == 0 or rd == 0 or li == 0 or ri == 0 and lo == 0 or ro == 0 or cl == 0 or ml == 0):
raise ValueError("Error: A state is not specified or invalid entry")
if not(ld == 1 or rd == 1 or li == 1 or ri == 1 and lo == 1 or ro == 1 or cl == 1 or ml == 1):
raise ValueError("Error: A state is not specified or invalid entry")
leftDoor = (ld or li or lo)
rightDoor = (rd or ri or ro)
if (li and cl):
leftDoor = 0
if (ri and cl):
rightDoor = 0
if leftDoor and rightDoor and not ml and gs == 'P':
print("left door and right door open")
elif leftDoor and not ml and gs == 'P':
print("open left door")
elif rightDoor and not ml and gs == 'P':
print("open right door")
else:
print("both doors stay closed")
open_doors([0,0,1,6,0,0,1,0,'2'])
Raises ValueError if not every state is specified,
the switches and locks are not 0 or 1 or the gs is
not one of ('P' 'R', 'N', '2', '3' 'D')
python python-3.x
python python-3.x
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
Chukas Ebuka
32
32
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Chukas Ebuka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as unclear what you're asking by Yu Hao, usr2564301, tripleee, ewolden, Madhur Bhaiya 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by Yu Hao, usr2564301, tripleee, ewolden, Madhur Bhaiya 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
So you just want to make your code more concise? Also, is the singleandstatement in your if condition a typo?
– DocDriven
2 days ago
1
look upany()andall()
– Chris_Rands
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago
add a comment |
So you just want to make your code more concise? Also, is the singleandstatement in your if condition a typo?
– DocDriven
2 days ago
1
look upany()andall()
– Chris_Rands
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago
So you just want to make your code more concise? Also, is the single
and statement in your if condition a typo?– DocDriven
2 days ago
So you just want to make your code more concise? Also, is the single
and statement in your if condition a typo?– DocDriven
2 days ago
1
1
look up
any() and all()– Chris_Rands
2 days ago
look up
any() and all()– Chris_Rands
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I would do
if any((i != 0 and i != 1) for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
This is short and concise.
It can be even changed to
valid = set((0, 1))
if any(i not in valid for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
bit I am not sure if that is better performance- or readability-wise.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I would do
if any((i != 0 and i != 1) for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
This is short and concise.
It can be even changed to
valid = set((0, 1))
if any(i not in valid for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
bit I am not sure if that is better performance- or readability-wise.
add a comment |
up vote
1
down vote
accepted
I would do
if any((i != 0 and i != 1) for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
This is short and concise.
It can be even changed to
valid = set((0, 1))
if any(i not in valid for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
bit I am not sure if that is better performance- or readability-wise.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I would do
if any((i != 0 and i != 1) for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
This is short and concise.
It can be even changed to
valid = set((0, 1))
if any(i not in valid for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
bit I am not sure if that is better performance- or readability-wise.
I would do
if any((i != 0 and i != 1) for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
This is short and concise.
It can be even changed to
valid = set((0, 1))
if any(i not in valid for i in state):
raise ValueError("Error: A state is not specified or invalid entry")
bit I am not sure if that is better performance- or readability-wise.
answered 2 days ago
glglgl
64.6k788163
64.6k788163
add a comment |
add a comment |
So you just want to make your code more concise? Also, is the single
andstatement in your if condition a typo?– DocDriven
2 days ago
1
look up
any()andall()– Chris_Rands
2 days ago
I just want to enter 0 or 1 and if it is not 0 or 1 it should raise ValueError
– Chukas Ebuka
2 days ago