Python 如果没有,如何缩短这些代码?
第一个代码不起作用,但第二个代码起作用。如何将第二个代码缩短为第一个代码? 您能缩短整个代码吗?Python 如果没有,如何缩短这些代码?,python,python-3.x,Python,Python 3.x,第一个代码不起作用,但第二个代码起作用。如何将第二个代码缩短为第一个代码? 您能缩短整个代码吗? 它应该继续代码您可以这样做: if not (argument1.isalpha() and argument2.isalpha() and argument3.isalpha()): print(("Argument {} {} {} are not a word. All arguments should be word").format(argument1, argument2, ar
它应该继续代码您可以这样做:
if not (argument1.isalpha() and argument2.isalpha() and argument3.isalpha()):
print(("Argument {} {} {} are not a word. All arguments should be word").format(argument1, argument2, argument3))
elif not (argument1 and argument2).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument1, argument2))
elif not (argument1 and argument3).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument1, argument3))
elif not (argument2 and argument3).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument2, argument3))
elif not argument1.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument1))
elif not argument2.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument2))
elif not argument3.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument3))
else:
if [arg for arg in [argument1, argument2, argument3] if not arg.isalpha()]:
print("Argument {} is/are not a word(s). All arguments should be word".format(" ".join(listOfInvalidArgs)))
或者,如果您的参数在列表中,您可以缩短第一部分:
bad_arguments = []
if not argument1.isalpha():
bad_arguments.append(argument1)
if not argument2.isalpha():
bad_arguments.append(argument2)
if not argument3.isalpha():
bad_arguments.append(argument3)
if len(bad_arguments) == 1:
print(("Argument {} is not a word. All arguments should be words").format(bad_arguments[0]))
elif len(bad_arguments) > 1:
print(("Arguments {} are not words. All arguments should be words").format(" ".join(bad_arguments)))
else:
#all the arguments are words do what you want here
试试这个,它还支持整数:
bad_arguments = []
for argument in arguments:
if not argument.isalpha():
bad_arguments.append(argument)
请注意,如果您使用的是Python3.6
或更高版本,则可以使用新格式的格式化字符串:
for i in [argument1,argument2,argument3]:
if not (isinstance(i,str) and i.isalpha()):
print('{} is not word'.format(i))
您可以将参数放入列表中,然后检查其中是否有不符合字母顺序的参数,然后打印出不正确的参数
print(f'{i} is not word')
AntiMatterDynamite的答案是好的,但是这甚至可以通过列表缩短一点:
argument1 = 'abc'
argument2 = 'abc 123'
argument3 = '123'
arguments = [argument1, argument2, argument3]
wrong_args = [not i.isalpha() for i in arguments]
if any(wrong_args):
print('Arguments {} are not alphabetic'.format(' '.join(str(idx+1) for idx, i in enumerate(wrong_args) if i)))
else:
print('Your arguments are correct')
或更短
argument1, argument2, argument3 = "a", "b", "0123"
listOfArgs = [argument1, argument2, argument3]
# get the list of invalid argumetns
listOfInvalidArgs = [arg for arg in listOfArgs if not arg.isalpha()]
# if the list is not empty
if listOfInvalidArgs:
print("Argument {} is/are not a word(s). All arguments should be words!".format(" ".join(listOfInvalidArgs)))
根据您的代码,看起来您只是在检查某些字符串参数是否不是字母 短版本的问题在于,您没有对字符串调用
isalpha()
,而是在字符串之间执行和
。如果您只需打印出(argument1、argument2和argument3)
的结果,您就会非常清楚地看到问题。您会注意到,结果不是布尔值(如您可能预期的那样),而是最后一个字符串的值
实际上,您在较长的代码示例中修复了此问题:(argument1.isalpha()和…
,而不是在整个块上
根据第二个代码块,您还需要确定哪些参数是错误的。虽然您可以对三个参数执行此操作,但最好创建一个更通用的解决方案,以便对任意数量的参数执行此操作。首先,您可以这样做:
if not (argument1.isalpha() and argument2.isalpha() and argument3.isalpha()):
print(("Argument {} {} {} are not a word. All arguments should be word").format(argument1, argument2, argument3))
elif not (argument1 and argument2).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument1, argument2))
elif not (argument1 and argument3).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument1, argument3))
elif not (argument2 and argument3).isalpha():
print(("Argument {} {} are not a word. All arguments should be word").format(argument2, argument3))
elif not argument1.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument1))
elif not argument2.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument2))
elif not argument3.isalpha():
print(("Argument {} is not a word. All arguments should be word").format(argument3))
else:
if [arg for arg in [argument1, argument2, argument3] if not arg.isalpha()]:
print("Argument {} is/are not a word(s). All arguments should be word".format(" ".join(listOfInvalidArgs)))
然后,您可以简单地检查返回的列表是否为空。如果不是,您可以使用列表中的所有参数打印消息
这可以通过使用列表进一步缩短:
def check_alphabetic(*arguments):
non_alphabetic = []
for arg in arguments:
if not arg.isalpha():
non_alphabetic.append(arg)
return non_alphabetic