如何将变量插入 "input" 函数

How to insert a variable into an "input" function

我想在 Python 的提示行中显示两个变量,如下所示:

a = 35
b = 11
captcha = int(input(a, '+', b '='))

它必须看起来像: 35 + 11 =

终端说有语法错误。有人可以修复我的语法吗?谢谢!

试试 f 字符串:

captcha =  int(input(f'{a} + {b} = '))

这里有一个更简单的方法来完成您想要做的事情: captcha = int(input(f"{a}+{b}="))

你有两个问题。

您错过了分隔 b= 的第三个逗号。 然后输入只接受一个参数。如果您尝试在函数中连接字符串,python 会认为您正在尝试添加多个参数。请尝试使用 f-strings

captcha = int(input(f'{a} + {b} ='))