Galo Gramma Asked:2020-06-20 20:17:08 +0000 UTC2020-06-20 20:17:08 +0000 UTC 2020-06-20 20:17:08 +0000 UTC 如何将字符串格式化为 2 个整数? 772 输入是字符串“283 + 5621”。 如何将字符串拆分为 2 个整数 - 283 和 5621。将输入的任何内容,例如“32 + 679” 并强制解释器添加这 2 个整数,甚至是小数? 运算可以是任意的,减法,除法等。 python 2 个回答 Voted Best Answer вася 2020-06-20T20:22:16Z2020-06-20T20:22:16Z import operator possible_operators = {'+': operator.add} # добавляйте другие операторы, если надо operation = '283 + 5621' splitted_operation = operation.split() operand_1 = int(splitted_operation[0]) operator = splitted_operation[1] operand_2 = int(splitted_operation[2]) print(possible_operators[operator](operand_1, operand_2)) Galo Gramma 2020-06-20T21:07:21Z2020-06-20T21:07:21Z 找到了更好的选择。 inputString = "2311 + 431" inputString = inputString.split() if inputString[1] == "+": print(int(inputString[0]) + int(inputString[1])) if inputString[1] == "-": print(int(inputString[0]) - int(inputString[1]))
找到了更好的选择。