Home > OS >  If input == list
If input == list

Time:12-17

So basically i'm trying to make it so that if user input is == "Blue" or "blue" it gives eye_res the value of 1. If not then give it the value of 0. What I've tried is:

Eye_color = input("Eye color: ")
A= (int["Blue","blue"])

if Eye_color in range (str(A)):
    eye_res = 1
else:
    eye_res = 0

print (eye_res)

CodePudding user response:

I believe this does what you are looking for:

Eye_color = input("Eye color: ")

if Eye_color in ["Blue", "blue"]:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

If you want to save the list first you can also do:

Eye_color = input("Eye color: ")
A = ["Blue", "blue"]

if Eye_color in A:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

Also, if you want them to be able to capitalize 'blue' any way and eye_res still be 1 then you can do:

Eye_color = input("Eye color: ")

if Eye_color.lower() == 'blue':
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

CodePudding user response:

You can compare against a set value by using modifiers on the input.

Eye_color = input("Eye color: ")

if Eye_color.lower() == “blue”:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

This makes any capitalisation of “blue” set eye_res to 1, while keeping the Eye_color variable as the exact input string.

CodePudding user response:

I didn't understand what your logic in A= (int["Blue","blue"]) Here is the code for what you want to achieve :

Eye_color = input("Eye color : ")
A = ["Blue", "blue"]
if Eye_color in A:
    eye_res = 1
else:
    eye_res = 0
print(eye_res)

Also one-liner solution to this :

print(1 if input("Eye color : ").lower() == "blue" else 0)

CodePudding user response:

You can do this:

eye_color = input("Eye color: ").lower()

eye_res = int(eye_color == 'blue')

CodePudding user response:

Consider utilizing str.lower and just comparing against 'blue':

eye_color = input('Eye color: ')
blue_eye_color = 'blue'

if eye_color.lower() == blue_eye_color:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

Example Usage 1:

Eye color: blue
1

Example Usage 2:

Eye color: Blue
1

Example Usage 3:

Eye color: BLUe
1

Example Usage 4:

Eye color: Green
0

CodePudding user response:

You can condense the answer to:

eye_res = int(input("Eye color: ") in ["Blue", "blue"])
  • Related