的作品是使用重复字符语法检查一个字符串包含相同字符的另一种方法。下面是一个PyUnit测试的代码片段,它演示了如何使用几个不同的字符串来实现,它们都包含一些逗号。
# simple test
test_string = ","
self.assertTrue(test_string.strip() == "," * len(test_string.strip()),
"Simple test failed")
# Slightly more complex test by adding spaces before and after
test_string = " ,,,,, "
self.assertTrue(test_string.strip() == "," * len(test_string.strip()),
"Slightly more complex test failed")
# Add a different character other than the character we're checking for
test_string = " ,,,,,,,,,a "
self.assertFalse(test_string.strip() == "," * len(test_string.strip()),
"Test failed, expected comparison to be false")
# Add a space in the middle
test_string = " ,,,,, ,,,, "
self.assertFalse(test_string.strip() == "," * len(test_string.strip()),
"Test failed, expected embedded spaces comparison to be false")
# To address embedded spaces, we could fix the test_string
# and remove all spaces and then run the comparison again
fixed_test_string = "".join(test_string.split())
print("Fixed string contents: {}".format(fixed_test_string))
self.assertTrue(fixed_test_string.strip() == "," * len(fixed_test_string.strip()),
"Test failed, expected fixed_test_string comparison to be True")
使用'for'循环。 – Blender
所有你需要做的就是跟踪最后一个字符并将其与当前循环进行比较,'return print(d [0])'也是无效的 –