在python中,您可以使用一个非常简单的if语句来查看用户输入的内容(或只是一个变量)是否在列表中:
myList = ["x", "y", "z"]
myVar = "x"
if myVar in x:
print("your variable is in the list.")
Run Code Online (Sandbox Code Playgroud)
我怎么能用Java做到这一点?
如果我说错了,我很抱歉,但我会尽力解释我想做什么.
是否可以在Python中执行此操作 -
class Character():
strength, skill = 0, 0
def foo(self, strength, skill):
if c1.strength > c2.strength:
#something here
c1 = Character()
c2 = Character()
c1.strength = 15
c2.strength = 13
Run Code Online (Sandbox Code Playgroud)
我真的不知道如何解释这个,但我想要做的是使用我在方法内部制作的两个实例中的变量?
该代码是否有效,或者还有其他什么?提前致谢.
有人可以用简单的方式为我解释这段代码.
prompts = ("Enter Strength Attribute, between 1 and 50: ", "Enter Skill Attribute, between 1 and 50: ") # I already now this so no need to explain it
answers = [int(input(p)) for p in prompts]
if not all(0 < a <=50 for a in answers):
# other code here
Run Code Online (Sandbox Code Playgroud)
它是发电机吗?
它是如何工作的?
提前感谢您的任何答案.