Python:我正在检查一个列表是否包含所有数字,如果是,将它们打印为tuple(a),如果不是,则打印一条错误消息。我的代码同时打印错误和元组(A)如果列表包含字母,我如何打印没有元组的错误消息?
#list_to_tuple function goes here
def list_to_tuple(a_list):
a = []
for i in a_list:
try:
i = int(i)
a.append(i)
except:
print("Error. Please enter only integers.")
print(tuple(a))
def main():
a_list = input("Enter elements of list separated by commas:").strip().split(',')
list_to_tuple(a_list)
main()发布于 2018-02-18 15:35:45
在打印错误的位置给出一个return命令。
except:
print("Error. Please enter only integers.")
return 就像这样。这应该是可行的。
https://stackoverflow.com/questions/48849334
复制相似问题