以下是一个示例代码,演示了如何比较一行中的两个值:
line = "Hello, World!"
value1 = "Hello"
value2 = "Python"
if value1 in line and value2 in line:
print(f"Both {value1} and {value2} are present in the line.")
elif value1 in line:
print(f"Only {value1} is present in the line.")
elif value2 in line:
print(f"Only {value2} is present in the line.")
else:
print("Neither value1 nor value2 is present in the line.")
上述代码首先定义了一个字符串变量line
,然后定义了两个要比较的值value1
和value2
。
使用if
条件语句和in
关键字,我们可以检查value1
和value2
是否都在line
中出现,或者只有一个出现,或者两者都没有出现。根据不同的情况,代码会输出相应的结果。
请注意,上述代码是使用Python编写的示例代码。如果您在其他编程语言中使用类似的逻辑,可能会有一些语法和语义上的差异。
上一篇:比较一行和一个列表