定义函数时出现语法错误

时间:2020-05-23 01:41:07

标签: python syntax-error

第5行的%sum2出现语法错误:

def sum2(l3,x1,x2):
     n1 = (x1.get())
     n2 = (x2.get())
     sum2 = int(n1) + int(n2)
     l3.config(text='Sum of these two numbers is: %d', %sum2)
     return

链接到错误消息

Link to error message

3 个答案:

答案 0 :(得分:1)

根据您发布的代码,您可能需要将第5行更改为:

l3.config(text=('Sum of these two numbers is: %d' % sum2))

答案 1 :(得分:0)

如果您查看l3.config(text='Sum of these two numbers is: %d', %sum2),将会看到您正在将文本设置为等于两项'Sum of these two numbers is: %d' {{1 }}。格式化的实际方法是这样的:%sum2

答案 2 :(得分:0)

当然,阿瑟斯里贝罗是对的。 作为信息,%用于将数字的模数乘以另一个: Python中10%2的输出为0,而11%2的输出为1。 因此,您必须在%和sum2之间保留一个空格。