[英]If input == list
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_res的值1。如果不是,那么给它0的值。我试过的是:
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)
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_res
仍然为 1,那么您可以执行以下操作:
Eye_color = input("Eye color: ")
if Eye_color.lower() == 'blue':
eye_res = 1
else:
eye_res = 0
print(eye_res)
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.这使得“blue”的任何大写都将
eye_res
设置为 1,同时将Eye_color
变量保持为精确的输入字符串。
I didn't understand what your logic in A= (int["Blue","blue"])
Here is the code for what you want to achieve:我不明白您在
A= (int["Blue","blue"])
中的逻辑是什么,这是您想要实现的代码:
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)
You can do this:你可以这样做:
eye_color = input("Eye color: ").lower()
eye_res = int(eye_color == 'blue')
Consider utilizing str.
lower
考虑使用
str. lower
and just comparing against str. lower
'blue'
: str. lower
,只是与'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:示例用法 1:
Eye color: blue
1
Example Usage 2:示例用法 2:
Eye color: Blue
1
Example Usage 3:示例用法 3:
Eye color: BLUe
1
Example Usage 4:示例用法 4:
Eye color: Green
0
You can condense the answer to:您可以将答案浓缩为:
eye_res = int(input("Eye color: ") in ["Blue", "blue"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.